【maven系列】:maven构建模块化项目之SpringMVC整合Mybatis (01)
一、整合所需jar包
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring-version}</version> </dependency> <!-- 整合mybatis所需jar包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis.spring-version}</version> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis-version}</version> </dependency> <!-- c3p0数据源 --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>${c3p0-version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.9</version> </dependency>
<properties></properties>标签在上一张介绍过,在这里只添加了一些新的jar包,不清楚的朋友点击这里---》 maven构建模块化项目之SpringMVC (02)
<!-- jar包管理--> <properties> <mybatis-version>3.2.8</mybatis-version> <mybatis.spring-version>1.2.2</mybatis.spring-version> <c3p0-version>0.9.1.2</c3p0-version> </properties>
二、MVC整合MyBatis
1、工程目录结构,src/main/resources/下根据不同的框架或者配置建立不同的文件夹方便管理。
2、spring-mybatis.xml Spring整合mybatis的xml配置信息
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <!-- 数据源c3p0 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClassName}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="maxPoolSize" value="20" /> <property name="minPoolSize" value="5" /> <property name="initialPoolSize" value="3" /> <property name="acquireIncrement" value="2" /> </bean> <!-- sessionFactory 将spring和mybatis整合 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" p:dataSource-ref="dataSource" p:configLocation="classpath:mybatis/mybatis-config.xml" /> <!-- spring与mybatis整合配置,扫描所有dao --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" p:basePackage="com.nc.rms.dao" p:sqlSessionFactoryBeanName="sqlSessionFactory"/> <!-- Spring配置事物 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource"/> <tx:advice id="Core_TxAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" read-only="true" /> <tx:method name="find*" read-only="true" /> <tx:method name="exists*" read-only="true" /> <tx:method name="*" propagation="REQUIRED" rollback-for="Exception" /> </tx:attributes> </tx:advice> <aop:config proxy-target-class="true"> <aop:pointcut id="txPointcut" expression="execution(* com.nc.rms.service.*.*(..))" /> <aop:advisor advice-ref="Core_TxAdvice" pointcut-ref="txPointcut" /> </aop:config> </beans>
3、在上面的代码中我们可以看到p:configLocation="classpath:mybatis/mybatis-config.xml" 这段代码,这个引入的xml是用来配置 mybatis的一信息,接下来看看mybatis-config.xml,<package name="com.nc.rms.bean"/> 这段代码相当于路径一样。后面我们可以看到这里的应用
<?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> <!-- 扫描Bean路径下的所有文件 --> <typeAliases> <package name="com.nc.rms.bean"/> </typeAliases> </configuration>
4、配置mapper.xml,因为博主有点小小的强迫症,个人觉得将xml放到Dao层会显得Dao层非常臃肿、不清晰故将xml文件与Dao层分离开来
<resultMap id="UserResultMap" type="User" > 这里的type="User" 可以结合上面所提到的<package name="com.nc.rms.bean"/>去理解,之前配置了com.nc.rms.bean所以我们在type这里就不需要在去写User的全路径了。
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0/" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- 命名空间 --> <mapper namespace="com.nc.rms.dao.UserMapper"> <!-- 实体与数据库表映射 column : 数据库字段名 property:实体属性名 --> <resultMap id="UserResultMap" type="User" > <id column="id" property="id" /> <result column="username" property="name"/> <result column="data_status" property="status"/> <result column="password" property="password"/> </resultMap> <select id="findAll" resultMap="UserResultMap"> SELECT * FROM cas_user </select> </mapper>
三、java后台代码
1、Controller代码,这里需要提醒下因为我们是maven模块化项目 在这里我们是获取不到
IUserService.java 文件的 我们需要在 web层的 pom.xml文件中引入我们的service层
package com.nc.rms.manage.web.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.nc.rms.bean.User; import com.nc.rms.service.IUserService; @Controller public class UserController { @Autowired private IUserService userService; @RequestMapping("/toUserHomePage") public ModelAndView toUserHomePage(){ List<User> users = userService.findAll(); System.out.println("SpringMVC:"+users.size()); return new ModelAndView("Helloworld"); } }
web层 pom.xml
<dependency> <groupId>com.nc.rms</groupId> <artifactId>rms-service</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
2、Dao层代码
package com.nc.rms.dao; import java.util.List; import org.springframework.stereotype.Repository; import com.nc.rms.bean.User; @Repository public interface UserMapper{ /** * 查询全部 */ public List<User> findAll(); }
3、service层代码
package com.nc.rms.service; import java.util.List; import com.nc.rms.bean.User; /** * 用户Service * @author Carl * */ public interface IUserService { /** * 查询全部 * @return */ public List<User> findAll(); }
4、service实现类,service模块下是没有引入任何jar包的 所以会找不到Spring注解,所以我们需要在service模块的pom.xml添加Spring相应的jar包
package com.nc.rms.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.nc.rms.bean.User; import com.nc.rms.dao.UserMapper; import com.nc.rms.service.IUserService; @Service public class UserServiceImpl implements IUserService{ @Autowired private UserMapper userMapper; public List<User> findAll() { return userMapper.findAll(); } }
service模块pom.xml中引入jar
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring-version}</version> </dependency>
5、将Spring整合myBatis的xml文件引入到web.xml中,在web.xml中找到这段代码,将spring-mybatis.xml 引进来
<context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:spring/spring.xml, classpath:mybatis/spring-mybatis.xml </param-value> </context-param>
四、测试运行
在Controller层打印了下List<User>集合的长度,在这里可以看到运行结果。