sm整合
1 pom.xml 添加依赖 数据库包要对应版本 2 <dependencies> 3 <dependency> 4 <groupId>junit</groupId> 5 <artifactId>junit</artifactId> 6 <version>4.12</version> 7 </dependency> 8 <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> 9 <dependency> 10 <groupId>mysql</groupId> 11 <artifactId>mysql-connector-java</artifactId> 12 <version>5.1.47</version> 13 </dependency> 14 <dependency> 15 <groupId>org.mybatis</groupId> 16 <artifactId>mybatis</artifactId> 17 <version>3.5.2</version> 18 </dependency> 19 <dependency> 20 <groupId>org.springframework</groupId> 21 <artifactId>spring-webmvc</artifactId> 22 <version>5.2.0.RELEASE</version> 23 </dependency> 24 <!--spring操作数据库的话,还需要一个spring-jdbc--> 25 <dependency> 26 <groupId>org.springframework</groupId> 27 <artifactId>spring-jdbc</artifactId> 28 <version>5.2.0.RELEASE</version> 29 </dependency> 30 <dependency> 31 <groupId>org.aspectj</groupId> 32 <artifactId>aspectjweaver</artifactId> 33 <version>1.8.13</version> 34 </dependency> 35 <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> 36 <dependency> 37 <groupId>org.mybatis</groupId> 38 <artifactId>mybatis-spring</artifactId> 39 <version>2.0.3</version> 40 </dependency> 41 <dependency> 42 <groupId>org.projectlombok</groupId> 43 <artifactId>lombok</artifactId> 44 <version>1.18.10</version> 45 </dependency> 46 </dependencies> 47 资源过滤 48 <build> 49 <resources> 50 <resource> 51 <directory>src/main/resources</directory> 52 <includes> 53 <include>**/*.properties</include> 54 <include>**/*.xml</include> 55 </includes> 56 </resource> 57 <resource> 58 <directory>src/main/java</directory> 59 <includes> 60 <include>**/*.properties</include> 61 <include>**/*.xml</include> 62 </includes> 63 <filtering>true</filtering> 64 </resource> 65 </resources> 66 </build>
1 实体类 2 3 @Data 4 public class User { 5 private int id; 6 private String name; 7 private String pwd; 8 }
1 接口 2 public interface UserMapper { 3 /*查詢*/ 4 List<User> selectUser(); 5 }
1 实现类 2 public class UserMapperImpl implements UserMapper { 3 4 //我们的所有操作,在原来都使用Session来执行 现在都使用SqlSessionTemlate 5 private SqlSessionTemplate sqlSession; 6 public void setSqlSession(SqlSessionTemplate sqlSession){ 7 this.sqlSession = sqlSession; 8 } 9 10 @Override 11 public List<User> selectUser() { 12 UserMapper mapper = sqlSession.getMapper(UserMapper.class); 13 return mapper.selectUser(); 14 } 15 }
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <mapper namespace="com.rzk.mapper.UserMapper"> 6 <select id="selectUser" resultType="user"> 7 select * from mybatis.user 8 </select> 9 </mapper>
顺便编写个mybatis的配置文件,加个别名,在Spring也可以做到
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <!--configguration核心配置文件--> 6 <configuration> 7 8 <!--给实体类起别名--> 9 <typeAliases> 10 <package name="com.rzk.pojo"/> 11 </typeAliases> 12 13 </configuration>
整合mybatis到Spring
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 https://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/aop 8 https://www.springframework.org/schema/aop/spring-aop.xsd"> 9 10 <!--DataSource:使用Spring的数据源替换mybatis的配置 11 这里使用Spring提供的JDBC 12 --> 13 <!--jdbc需要导Spring的包--> 14 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 15 <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 16 <property name="url" value="jdbc:mysql://localhost:3306/mybatis?userSSL=true&useUnicode=true&characterEncoding=UTF8"/> 17 <property name="username" value="root"/> 18 <property name="password" value="123456"/> 19 </bean> 20 21 22 <!--sqlSessionFactory--> 23 <!--mybatis方法文档有--> 24 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 25 <property name="dataSource" ref="dataSource"/> 26 <!--绑定Mybatis配置文件--> 27 <!--configLocation:mybatis配置文件的地址--> 28 <property name="configLocation" value="classpath:mybatis-config.xml"/> 29 <!--注册映射器--> 30 <property name="mapperLocations" value="classpath:com/rzk/mapper/*.xml"/> 31 </bean> 32 33 <!--创建sqlSessionFactory SqlSessionTemplate:就是我们使用的SqlSessionFactory --> 34 <!--constructor里面的ref sqlSessionFactory是对应上面bean的id--> 35 <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> 36 <constructor-arg index="0" ref="sqlSessionFactory"/> 37 </bean> 38 39 <!--注入--> 40 <bean id="userMapper" class="com.rzk.mapper.UserMapperImpl"> 41 <property name="sqlSession" ref="sqlSession"/> 42 </bean> 46 </beans>
测试
1 public class MyTest { 2 @Test 3 public static void main(String[] args)throws IOException { 4 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); 5 UserMapper userMapper = (UserMapper) context.getBean("userMapper2"); 6 for (User user : userMapper.selectUser()) { 7 System.out.println(user); 8 } 9 } 10 }