社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
之前一直没有用过maven和Mybatis最近自己搭建一个基于Maven的Spring+SpringMVC+Mybatis开发环境。
注:有的时候没有自己动手真正搭过环境(脱离教学的方式),可能有些地方的问题注意不到的。
会在介绍搭建的同时记录一些遇见的坑和一些知识点。
首先放上Maven配置文件。
1、POM.xml(至于maven里面支持的一些服务插件什么的可以自己另行百度)
注:
①、pom文件要放到项目根目录下
②、maven的文件结构
src/main/java 该source floder用来存放java代码
src/test/java 该source floder用来存放测试代码
src/main/webapp 该source floder用来存放jsp和web配置文件,相当于web项目的webroot
target 用来存放maven打包编译的文件
注:普通项目如何改为maven项目
①、将项目更改为maven结构
②、然后在该项目下执行mvn命令:mvn eclipse:eclipse 项目根目录必须有pom.xml否则maven无法生成相应目录结构
③、执行成功后在.project中相应位置添加如下配置
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
</buildCommand>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
④、在项目的properties选项中,找到Deployment Assembly选项更改项目发布时的路径(看是否如下图,如果上面一切顺利,一般不用手动修改)
至此,你的项目就转化为了maven项目了。
maven所需要的jar包可以在 这里 进行搜索,还能看到其依赖包
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>E-LearningSchool</groupId>
<artifactId>E-LearningSchool</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>E-LearningSchool</name>
<properties>
<junit.version>4.12</junit.version>
<spring.version>4.3.12.RELEASE</spring.version>
<mybatis.version>3.4.1</mybatis.version>
<mybatis.spring.version>1.3.1</mybatis.spring.version>
<mysql.version>5.1.38</mysql.version>
<commons.logging.version>1.2.17</commons.logging.version>
<fastjson.version>1.2.44</fastjson.version>
<aspectjweaver.version>1.8.13</aspectjweaver.version>
</properties>
<dependencies>
<!-- junit -->
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.0.2</version>
<scope>test</scope>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver springaop 依赖 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectjweaver.version}</version>
</dependency>
<!-- commons -->
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-email -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-collections/commons-collections -->
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax/javaee-api -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<!-- data source -->
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<!-- mybatis -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
<!-- mysql -->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- fastjson -->
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<!-- javaxmail -->
<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
<!-- <dependency> -->
<!-- <groupId>javax.mail</groupId> -->
<!-- <artifactId>mail</artifactId> -->
<!-- <version>1.4.7</version> -->
<!-- </dependency> -->
<!-- log -->
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${commons.logging.version}</version>
</dependency>
</dependencies>
</project>
Spring+SpringMVC的配置:
注:写在前面的话
一、在配置web.xml的时候寻找spring相关的配置文件:
1、没有手动指定(contextConfigLocation)路径的话,则默认去web-info下去找(spring默认找applicationContext.xml,springmvc默认找XXX-servlet.xml)。
2、如果我们把配置文件放到src下面,或者自定义包路径下的时候,可以使用classpath方式寻找。(classpath:后跟配置文件所在包的路径,可以使用通配符)
3、classpath会在当前项目去找,而classpath* 则会连所引用的包中也搜索
4、具体配置介绍请移步这里:点击打开链接
①、Web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/spring-context.xml</param-value>
</context-param>
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
②Spring配置
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.scarecrow.elearning.uac.service">
<!-- 如果service和controller在同一个父包中,在扫描父包的时候需要使用该句进行排除,springmvc中同样需要排除 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- 基本属性 url,user,pass -->
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!-- 配置初始化大小,最小增长,最大活动连接 -->
<property name="initialSize" value="${jdbc.initialSize}"/>
<property name="minIdle" value="${jdbc.minIdle}"/>
<property name="maxActive" value="${jdbc.maxActive}"/>
<!-- 获取连接超时时间,单位毫秒 -->
<property name="maxWait" value="60000"/>
<!-- 设置间隔多久进行一次检查关闭空闲链接,单位毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="30000"/>
<!-- 设置每个连接在池中的最小生存时间,单位毫秒 -->
<property name="minEvictableIdleTimeMillis" value="30000"/>
<property name="validationQuery" value="SELECT 'x'" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 如果用Oracle,则把poolPreparedStatements配置为true,mysql可以配置为false。-->
<!-- <property name="poolPreparedStatements" value="false" /> -->
<!-- <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> -->
<!-- 配置监控统计拦截的filters -->
<!-- <property name="filters" value="wall,stat" /> -->
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:com/scarecrow/elearning/uac/resources/*/*Mapper.xml"></property>
<property name="typeAliasesPackage" value="com.scarecrow.elearning.uac.entity"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.scarecrow.elearning.uac.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="upd*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="transactionPointcut" expression="execution(* com.scarecrow.elearning.uac.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut"/>
</aop:config>
</beans>
③、SpringMVC配置
<?xml version="1.0" encoding="UTF-8" ?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 默认的注解映射的支持,自动注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->
<mvc:annotation-driven />
<!-- 自动扫描的包名 -->
<context:component-scan base-package="com.scarecrow.elearning.uac.controller">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
<!-- 视图解释类 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
Mybatis的注意:
因为Mybatis的配置(包括mapper代理生成,mapper扫描等操作)交给了Spring进行管理()
SqlSessionFactoryBean--------->用来扫描mapper.xml文件的,以及对entity生成别名,以供mapper sql文件使用。
MapperScannerConfigurer-------->用来扫描mapper接口,并给其注入sqlsession
mybatisMapper配置:
注:mybatis的mapper.xml和对应的接口可以不放在同一个包目录下,在spring对其进行扫描的时候会根据 namespace 指定的接口去生成实例代理对象。所以,namespace,在这里不止是为了sql的隔离作用,更重要的是和对应接口关联起来。
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.scarecrow.elearning.uac.dao.UserMapper">
<select id="getUserById" parameterType="int" resultType="User">
select * from user where id = #{id}
</select>
</mapper>
其他:因为在用mysql6.0+的开发包的时候出现了server time zone 错误,设置serverTimeZone一直乱码无法解决所以换成了5的开发包。mysql用的是5.7的,需要指定ssl为false,不过不指定也不影响开发。
jdbc.url=jdbc:mysql://localhost:3306/elearning?useUnicode=true&characterEncoding=UTF-8&serverTimeZone=GMT&useSSL=false
最后:附上一个简陋的例子点击打开链接
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!