步骤
- 通过idea创建项目
- 集成spring-mvc
- 集成spring
- 集成c3p0
- 配置spring声明式事务管理
- 集成Mybatis
一、创建项目
-
选择maven-archetype-webapp:
create_project_01.png -
填写GroupId和ArtifactId,一路next-->finish即可
create_project_02.png -
src目录下创建java包并设置为source root,同样resources也要设置为resource root
图片.png -
创建项目结构包:
图片.png - 打开webapp下WEB-INF的web.xml可以看到idea自动生成的dtd是比较老的版本,需要改为3.0版本
可以打开apache下的conf\web.xml文件复制
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
-
修改默认的字节码编译版本,在.idea下的compiler.xml可以查看,默认是1.5修改为1.8:
图片.png
并且在pom.xml下修改
<build>
<finalName>example</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
到此就项目创建就完成了
二、集成spring-mvc
- 搜索org.springframework的Spring Web MVC
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
复制进 pom.xml文件中<dependencies> 标签中,并且将version提取到<properties>标签中,便于以后维护时管理版本:
<properties>
<spring.version>4.3.3.RELEASE</spring.version>
<mybatis.version>3.4.1</mybatis.version>
<mybatis.spring.version>1.3.0</mybatis.spring.version>
<cglib.version>3.2.2</cglib.version>
</properties>
- 依赖servlet和jstl,因为tomcat也有,所以定义一下作用域scope,容器提供:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
- 配置spring-mvc
在resources目录下创建spring-mvc.xml,配置等会再写,在这之前需要初始化spring-mvc配置到web.xml中。
web.xml:先定义一个servlet和servlet-mapping,
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!--contextConfigLocation为固定参数名称-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<!--定义为1,是为了和tomcat一起启动-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
spring-mvc.xml:
<!--使用注解请求映射-->
<mvc:annotation-driven/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--重定向时是否添加上下文路径-->
<property name="redirectContextRelative" value="true"/>
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--扫描所有控制器扫描包-->
<context:component-scan base-package="com.jzm.example.api.controller"/>
web.xml中配置请求和应答字符编码处理过滤器CharacterEncodingFilter,要配置在前面
<filter>
<description>请求和应答字符编码过滤器</description>
<filter-name>encoding-filter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encoding-filter</filter-name>
<servlet-name>spring-mvc</servlet-name>
</filter-mapping>
- 验证spring-mvc可用:
@Controller
public class LoginHandler {
@RequestMapping("/login.do")
public String login(String account, String password, HttpServletRequest httpRequest){
System.out.printf("我进来了");
return "index";
}
}
启动tomcat,访问 http://localhost/login.do,看到index.jsp界面即说明配置成功。
三、集成spring
由于集成spring-mvc时spring的基础包都已经导入进来不需额外依赖,只需配置一下,在resources目录下创建spring-context.xml文件进行配置:
首先在web.xml下配置spring监听器
<listener>
<description>启动spring容器</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-context.xml</param-value>
</context-param>
spring-context:
<!--扫描业务层所有组件 service-->
<context:component-scan base-package="com.jzm.example.api.function"/>
四、集成c3p0
- 添加依赖c3p0和jdbc-mysql
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
- 在spring-context中定义c3p数据源ComboPooledDataSource
<!--使用c3p0数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/vip"></property>
<property name="user" value="root"></property>
<property name="password" value="twgaqg"></property>
<!--最小访问数量-->
<property name="minPoolSize" value="1"></property>
<property name="maxPoolSize" value="5"></property>
<!--初始化获取多少个连接-->
<property name="initialPoolSize" value="5"/>
<!--每次连接数不够获取几条-->
<property name="acquireIncrement" value="1"/>
</bean>
五、配置spring声明式事务管理
- 依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
- spring-context中配置事务管理器:
<!--配置一个事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!--表示使用事务注解来控制事务-->
<tx:annotation-driven transaction-manager="transactionManager"/>
六、集成mybatis
1.添加依赖:mybatis、mybatis-spring、pagehelper、cglib:
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>${cglib.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.6</version>
</dependency>
- 配置:创建mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="cacheEnabled" value="true"/>
<setting name="defaultStatementTimeout" value="3000"/>
<setting name="proxyFactory" value="CGLIB"/>
<setting name="lazyLoadingEnabled" value="true"/>
</settings>
<!--代理和颜色加载必须要配置-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="mysql" />
<property name="offsetAsPageNum" value="true"/>
<property name="rowBoundsWithCount" value="true"/>
<property name="pageSizeZero" value="true"/>
</plugin>
</plugins>
</configuration>
- spring-context.xml配置SqlSessionFactoryBean
指定数据源和mybatis配置文件路径和mapper文件路径(mapper为在resources下创建的包)
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations">
<list>
<value>classpath:mapper/*.xml</value>
</list>
</property>
</bean>
- 在spring-context.xml配置扫描mapper生成dao(MapperScannerConfigurer)指定SqlSessionFactoryBean,指定要扫描的包
<!--扫描dao层-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.jzm.example.api.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
到此,集成完成。
网友评论