Mybatis+Spring的整合练习
一、建立数据库、建表
二、新建maven项目
三、添加依赖
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.0</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.13</version> </dependency> <!-- spring -->
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.3.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.3.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.2.RELEASE</version> </dependency>
<!-- c3p0 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> </dependencies>
四、建立包,搭建项目架构
五、根据数据库写对应的pojo类
package com.dongtian.MyBatis_Spring.pojo; import java.util.Date; public class User { private int id; private String username; private Date birthday; private String sex; private String address; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", birthday=" + birthday + ", sex=" + sex + ", address=" + address + "]"; } }
六、写映射器和接口
package com.dongtian.MyBatis_Spring.mapper; import org.springframework.stereotype.Repository; import com.dongtian.MyBatis_Spring.pojo.User; @Repository public interface UserMapper { public void insertUser(User user); }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.dongtian.MyBatis_Spring.mapper.UserMapper"> <insert id="insertUser" parameterType="com.dongtian.MyBatis_Spring.pojo.User"> insert into user(username,sex,address) values(#{username},#{sex},#{address}) </insert> </mapper>
七、写业务层接口和接口实现类
package com.dongtian.MyBatis_Spring.service; import com.dongtian.MyBatis_Spring.pojo.User; public interface UserService { public void insertUser(User user); }
package com.dongtian.MyBatis_Spring.serviceImp; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.dongtian.MyBatis_Spring.mapper.UserMapper; import com.dongtian.MyBatis_Spring.pojo.User; import com.dongtian.MyBatis_Spring.service.UserService; @Service public class UserServiceImp implements UserService{ @Autowired private UserMapper userMapper = null; @Transactional(propagation = Propagation.REQUIRES_NEW, isolation=Isolation.READ_COMMITTED ) public void insertUser(User user) { userMapper.insertUser(user); } }
八、简单测试
package com.dongtian.MyBatis_Spring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.dongtian.MyBatis_Spring.pojo.User; import com.dongtian.MyBatis_Spring.service.UserService; public class TestMyDemo { public static void main(String[] args) { //加载配置文件 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); //获取代理对象 UserService userService = applicationContext.getBean(UserService.class); //通过接口对象调用接口方法 User user = new User(); user.setUsername("王力宏"); user.setSex("男"); user.setAddress("上海"); userService.insertUser(user); System.out.println(user); } }
九、流程逻辑比较简单:
通过拿到业务层接口的代理对象,代理对象调用接口中的方法,接口方法调用映射器中的方法,由mapper操作数据库。
十、重点在配置文件中
(1)sqlMapConfig.xml : 大部分事情都交给了spring
<?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> <mappers> <package name="com.dongtian.MyBatis_Spring.mapper"/> </mappers> </configuration>
(2)applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 启动扫描机制 --> <context:annotation-config/> <context:component-scan base-package="com.dongtian.MyBatis_Spring.*"/> <!-- 加载配置文件 --> <context:property-placeholder location="classpath:db.properties" /> <bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- sqlSessinFactory --> <bean id="sqlSFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="sqlMapConfig.xml" /> <property name="dataSource" ref="datasource" /> </bean> <!-- mapper批量扫描,从mapper包中扫描出mapper接口,自动创建代理对象并且在spring容器中注册 自动扫描出来的mapper的bean的id为mapper类名(首字母小写) --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.dongtian.MyBatis_Spring.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSFactory"/> <property name="annotationClass" value="org.springframework.stereotype.Repository"></property> </bean> <!-- 事务管理器配置数据源事务 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="datasource"></property> </bean> <!-- 使用注解来定义事务 --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>