1、創建數據庫、建表java
2、新建maven項目mysql
3、添加依賴spring
<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>
4、創建包,搭建項目架構sql
5、根據數據庫寫對應的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 + "]"; } }
6、寫映射器和接口spring-mvc
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>
7、寫業務層接口和接口實現類mybatis
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); } }
8、簡單測試架構
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); } }
9、流程邏輯比較簡單:mvc
經過拿到業務層接口的代理對象,代理對象調用接口中的方法,接口方法調用映射器中的方法,由mapper操做數據庫。app
10、重點在配置文件中
(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>