java

sm整合

rzk · 4月7日 · 2020年本文共1648个字 · 预计阅读6分钟133次已读
 1 pom.xml  添加依赖    数据库包要对应版本
 2     
 3   睿共享      
 4             junit
 5             junit
 6             4.12
 7         
 8         
 9         
10             mysql
11             mysql-connector-java
12             5.1.47
13         
14         
15             org.mybatis
16             mybatis
17             3.5.2
18         
19         
20             org.springframework
21             spring-webmvc
22             5.2.0.RELEASE
23         
24         
25         
26             org.springframework
27             spring-jdbc
28             5.2.0.RELEASE
29         
30         
31             org.aspectj
32             aspectjweaver
33             1.8.13
34         
35         
36         
37             org.mybatis
38             mybatis-spring
39             2.0.3
40         
41         
42             org.projectlombok
43             lombok
44             1.18.10
45         
46     
47 资源过滤
48     
49         
50             
51                 src/main/resources
52                 
53                     **/*.properties
54                     **/*.xml
55                 
56             
57             
58                 src/main/java
59                 
60                     **/*.properties
61                     **/*.xml
62                 
63                 true
64             
65         
66     

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 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 selectUser() {
12         UserMapper mapper = sqlSession.getMapper(UserMapper.class);
13         return mapper.selectUser();
14     }
15 }

1 "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 namespace="com.rzk.mapper.UserMapper">
6     <select id="selectUser" resultType="user">
7         select * from mybatis.user
8     select>
9 

顺便编写个mybatis的配置文件,加个别名,在Spring也可以做到

 1 "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 
 6 
 7 
 8     
 9     
10         "com.rzk.pojo"/>
11     
12 
13 

整合mybatis到Spring

 1 "1.0" encoding="UTF-8"?>
 2 "http://www.springframework.org/schema/beans"
 3        xmlns_xsi="ht睿共享tp://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     
13     
14     "dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
15         "driverClassName" value="com.mysql.jdbc.Driver"/>
16         "url" value="jdbc:mysql://localhost:3306/mybatis?userSSL=true&useUnicode=true&characterEncoding=UTF8"/>
17         "username" value="root"/>
18         "password" value="123456"/>
19     
20 
21 
22     
23     
24     "sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
25         "dataSource" ref="dataSource"/>
26         
27         
28         "configLocation" value="classpath:mybatis-config.xml"/>
29         
30         "mapperLocations" value="classpath:com/rzk/mapper/*.xml"/>
31     
32 
33     
34     
35     "sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
36         "0" ref="sqlSessionFactory"/>
37     
38 
39     
40     "userMapper" class="com.rzk.mapper.UserMapperImpl">
41         "sqlS睿共享ession" ref="sqlSession"/>
42     
46 

测试

 1 public class MyTest {
 2     @Test
 3     public sta睿共享tic void main(String[] args)throws IOExce睿共享ption {
 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 }

0 条回应