spring整合mybatis
先在IDEA中创建一个Maven项目,加入常用依赖:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>spring-mybatis</groupId> 8 <artifactId>spring-mybatis</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 <dependencies> 11 <dependency> 12 <groupId>junit</groupId> 13 <artifactId>junit</artifactId> 14 <version>4.12</version> 15 </dependency> 16 <dependency> 17 <groupId>mysql</groupId> 18 <artifactId>mysql-connector-java</artifactId> 19 <version>5.1.47</version> 20 </dependency> 21 <dependency> 22 <groupId>org.mybatis</groupId> 23 <artifactId>mybatis</artifactId> 24 <version>3.5.2</version> 25 </dependency> 26 <dependency> 27 <groupId>org.mybatis</groupId> 28 <artifactId>mybatis-spring</artifactId> 29 <version>2.0.2</version> 30 </dependency> 31 <dependency> 32 <groupId>org.springframework</groupId> 33 <artifactId>spring-webmvc</artifactId> 34 <version>5.1.8.RELEASE</version> 35 </dependency> 36 <dependency> 37 <groupId>org.springframework</groupId> 38 <artifactId>spring-jdbc</artifactId> 39 <version>5.1.8.RELEASE</version> 40 </dependency> 41 <dependency> 42 <groupId>aspectj</groupId> 43 <artifactId>aspectjweaver</artifactId> 44 <version>1.5.3</version> 45 </dependency> 46 <dependency> 47 <groupId>org.projectlombok</groupId> 48 <artifactId>lombok</artifactId> 49 <version>1.18.8</version> 50 </dependency> 51 <dependency> 52 <groupId>org.springframework</groupId> 53 <artifactId>spring-tx</artifactId> 54 <version>5.1.8.RELEASE</version> 55 </dependency> 56 </dependencies> 57 <build> 58 <resources> 59 <resource> 60 <directory>src/main/java</directory> 61 <includes> 62 <include>**/*.xml</include> 63 </includes> 64 <filtering>true</filtering> 65 </resource> 66 </resources> 67 </build> 68 69 </project>
创建一个pojo类:
1 package com.cai.pojo; 2 3 import lombok.AllArgsConstructor; 4 import lombok.Data; 5 import lombok.NoArgsConstructor; 6 7 @Data 8 @AllArgsConstructor 9 @NoArgsConstructor 10 public class User { 11 private Integer id; 12 private String name; 13 private String pwd; 14 }
然后新建一个映射器接口,里面就是一些增删改查的操作:
1 package com.cai.mapper; 2 import com.cai.pojo.User; 3 import java.util.List; 4 public interface UserMapper { 5 List<User> selectUser(); 6 int delete(int id); 7 int add(User user); 8 int update(User user); 9 }
然后创建对应的xml映射文件,这里映射文件和接口在同一个包下面:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <!-- 配置mybatis --> 6 7 <mapper namespace="com.cai.mapper.UserMapper"> 8 <select id="selectUser" resultType="user"> 9 select * from mybatis.user 10 </select> 11 12 <insert id="add" parameterType="user"> 13 insert into mybatis.user(id,name,pwd) VALUES (#{id},#{name},#{pwd}) 14 </insert> 15 16 <delete id="delete" parameterType="int"> 17 deletes from mybatis.user where id=#{id} 18 </delete> 19 </mapper>
因为这是spring整合mybatis,所以对接口必须进行进行实现:
1 package com.cai.mapper; 2 3 import com.cai.pojo.User; 4 import org.mybatis.spring.SqlSessionTemplate; 5 6 import java.util.List; 7 8 public class UserMapperImpl implements UserMapper { 9 10 private SqlSessionTemplate sqlSession; 11 12 public void setSqlSession(SqlSessionTemplate sqlSessionTemplate) { 13 this.sqlSession = sqlSessionTemplate; 14 } 15 16 public List<User> selectUser(){ 17 UserMapper user = sqlSession.getMapper(UserMapper.class); 18 return user.selectUser(); 19 } 20 21 public int delete(int i) { 22 return 0; 23 } 24 public int add(User user) { 25 return 0; 26 } 27 28 public int update(User user) { 29 return 0; 30 } 31 }
然后就需要把这个实现类交给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" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <!--DataSource:使用Spring的数据源代替mybatis配置--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis?useSSL=false"/> <property name="username" value="root"/> <property name="password" value="123"/> </bean> <!--sqlSessionFaction--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!--绑定mybatis配置文件--> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="mapperLocations" value="classpath:com/cai/mapper/*.xml"/> </bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <!--只能使用构造器注入sqlSession--> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean> <bean id="userMapper" class="com.cai.mapper.UserMapperImpl"> <property name="sqlSession" ref="sqlSession"/> <!--这里name就是UserMapperImpl中的sqlSession--> </bean> </beans>
后还要有一个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"> <!-- 配置mybatis --> <configuration> <!-- 配置别名 --> <typeAliases> <package name="com.cai.pojo"/> </typeAliases> </configuration>
然后就可以进行测试:
import com.cai.mapper.UserMapper;import com.cai.pojo.User;import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException;import java.util.List; public class MyTest { public static void main(String[] args) throws IOException { ApplicationContext app = new ClassPathXmlApplicationContext("spring-dao.xml"); UserMapper user = app.getBean("userMapper",UserMapper.class); for (User usr : user.selectUser()){ System.out.println(String.valueOf(usr)); } } }