【Spring5】Spring5基础教程(8)
前言:
本文内容:回顾MyBatis、整合Mybatis方式一、整合MyBatis方式二
推荐免费Spring5基础教程视频:【狂神说Java】Spring5最新完整教程IDEA版通俗易懂_哔哩哔哩_bilibili
回顾Mybatis
整合前准备
-
导入相关jar包
- junit
- mybatis
- mysql
- spring相关
- aop
- mybatis-spring
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60<!--要引入的jar包-->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.19</version>
</dependency>
<!-- 连接spring-jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.8</version>
</dependency>
<!-- mybatis-spring-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
</dependencies>
<!-- 解决资源导出问题-->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources> -
编写配置文件
-
测试
回顾Mybatis
-
编写实体类
1
2
3
4
5
6
7
8
9
10
11
12
13
14package com.jokerdig.pojo;
import lombok.Data;
/**
* @author Joker大雄
* @data 2022/5/23 - 13:03
**/
public class User {
private int id;
private String name;
private String pwd;
} -
编写核心配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<configuration>
<!-- 别名-->
<typeAliases>
<package name="com.jokerdig.pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper class="com.jokerdig.mapper.UserMapper"/>
</mappers>
</configuration> -
编写接口
1
2
3
4
5
6
7
8
9
10
11
12
13package com.jokerdig.mapper;
import com.jokerdig.pojo.User;
import java.util.List;
/**
* @author Joker大雄
* @data 2022/5/23 - 13:25
**/
public interface UserMapper {
public List<User> selectUser();
} -
编写Mapper.xml
1
2
3
4
5
6
7
8
9
10
11
<mapper namespace="com.jokerdig.mapper.UserMapper">
<!-- 查询用户 来测试缓存-->
<select id="selectUser" resultType="user">
select * from user
</select>
</mapper> -
测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38package com.jokerdig.test;
import com.jokerdig.mapper.UserMapper;
import com.jokerdig.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* @author Joker大雄
* @data 2022/5/23 - 13:29
**/
public class SpringTest {
public void test9() throws IOException {
String resource = "mybatis-config.xml";
InputStream in = Resources.getResourceAsStream(resource);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = factory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> users = mapper.selectUser();
for (User user : users) {
System.out.println(user);
}
}
}运行结果
1
2
3
4
5
6User(id=1, name=admin, pwd=123)
User(id=2, name=abc, pwd=123)
User(id=3, name=def, pwd=123)
User(id=5, name=小呆瓜, pwd=33333)
Process finished with exit code 0
整合Mybatis方式一
在上方回顾代码的基础上做整合
使用SqlSessionTemplate
实现步骤
-
编写数据源
-
创建sqlSessionFactory
-
sqlSessionTemplate
applicationContext.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 1. datasource 使用spring数据源代替mybatis数据源-
DriverManagerDataSource
-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!-- 2. sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 3. 绑定mybatis-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/jokerdig/mapper/*.xml"/>
</bean>
<!-- 4. sqlSessionTemplate 就是sqlSession-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!-- 这里只能使用构造器注入 因为它没有set方法-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<!-- 5. 引入UserMapperImpl 使用 SqlSessionTemplate -->
<bean id="userMapper" class="com.jokerdig.mapper.UserMapperImpl">
<property name="sqlSessionTemplate" ref="sqlSession"/>
</bean>
</beans> -
需要给接口加实现类
UserMapperImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25package com.jokerdig.mapper;
import com.jokerdig.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;
import java.util.List;
/**
* @author Joker大雄
* @data 2022/5/23 - 14:06
**/
public class UserMapperImpl implements UserMapper{
// 我们所有操作都是用SqlSessionTemplate
private SqlSessionTemplate sqlSessionTemplate;
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
// 查询所有
public List<User> selectUser() {
UserMapper mapper = sqlSessionTemplate.getMapper(UserMapper.class);
return mapper.selectUser();
}
} -
测试
1
2
3
4
5
6
7
8
9
10
11
12
public void test10(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
List<User> users = userMapper.selectUser();
for (User user : users) {
System.out.println(user);
}
}运行结果
1
2
3
4
5
6User(id=1, name=admin, pwd=123)
User(id=2, name=abc, pwd=123)
User(id=3, name=def, pwd=123)
User(id=5, name=小呆瓜, pwd=33333)
Process finished with exit code 0
整合Mybatis方式二
使用SqlSessionDaoSupport
实现步骤
-
编写Mapper的实现类,并继承
SqlSessionDaoSupport
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18package com.jokerdig.mapper;
import com.jokerdig.pojo.User;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import java.util.List;
/**
* @author Joker大雄
* @data 2022/5/23 - 14:23
**/
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper{
// 继承SqlSessionDaoSupport 可以直接getSqlSession()
public List<User> selectUser() {
return getSqlSession().getMapper(UserMapper.class).selectUser();
}
} -
编写spring配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- datasource 使用spring数据源代替mybatis数据源-
DriverManagerDataSource
-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!-- sqlSessionFactory-->
<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/jokerdig/mapper/*.xml"/>
</bean>
<!-- sqlSessionTemplate 就是sqlSession-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!-- 这里只能使用构造器注入 因为它没有set方法-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<!-- 引入UserMapperImpl 使用 SqlSessionTemplate-->
<bean id="userMapper" class="com.jokerdig.mapper.UserMapperImpl">
<property name="sqlSessionTemplate" ref="sqlSession"/>
</bean>
<!-- 引入UserMapperImpl2 使用SqlSessionDaoSupport -->
<bean id="userMapper2" class="com.jokerdig.mapper.UserMapperImpl2">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
</beans> -
测试
1
2
3
4
5
6
7
8
9
10
11
12
public void test11(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper = context.getBean("userMapper2", UserMapper.class);
List<User> users = userMapper.selectUser();
for (User user : users) {
System.out.println(user);
}
}运行结果
1
2
3
4
5
6User(id=1, name=admin, pwd=123)
User(id=2, name=abc, pwd=123)
User(id=3, name=def, pwd=123)
User(id=5, name=小呆瓜, pwd=33333)
Process finished with exit code 0
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Hey,Joker!
评论
ValineTwikoo