Spring集成MyBaits

加入依赖

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>Spring-MyBaits</artifactId>
<version>1.0-SNAPSHOT</version>

<name>Spring-MyBaits</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!--spring config-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.19</version>
</dependency>

<!--数据库事务-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>

<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.7</version>
</dependency>

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>

<!--阿里巴巴数据库连接池依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.9</version>
</dependency>
</dependencies>
<build>
<!--使得不在resoures文件下的依赖也可以被编译-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
<include>**/*.html</include>
</includes>
</resource>
</resources>
</build>
</project>

创建实体类

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
61
62
63
package com.zss.pojo;

/**
* @author zss
*/
public class Student {
private Integer id;
private String name;
private String email;
private Integer age;

public Student() {
}

public void setId(Integer id) {
this.id = id;
}

public void setName(String name) {
this.name = name;
}

public void setEmail(String email) {
this.email = email;
}

public void setAge(Integer age) {
this.age = age;
}

public Integer getId() {
return id;
}

public String getName() {
return name;
}

public String getEmail() {
return email;
}

public Integer getAge() {
return age;
}

public Student(Integer id, String name, String email, Integer age) {
this.id = id;
this.name = name;
this.email = email;
this.age = age;
}

@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
", age=" + age +
'}';
}
}

配置xml文件

studentdao.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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.zss.dao.StudentDAO">
<select id="getStudentList" resultType="com.zss.pojo.Student">
select * from student order by id;
</select>

<insert id="insertStudent">
insert into student values (#{id},#{name},#{email},#{age})
</insert>
</mapper>

主配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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>
<!--设置日志-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!--设置包名-->
<typeAliases>
<!--实体类所在的包名称-->
<package name="com.zss.pojo"/>
</typeAliases>

<!--该包下面的所有包配置文件-->
<mappers>
<package name="com.zss.dao"/>
</mappers>
</configuration>

Service方法

1
2
3
4
5
public interface StudentService {
List<Student> getStudents();

int insertStudent();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class StudentServiceImpl implements StudentService {

private StudentDAO studentDAO;

/**使用set注入进行赋值*/
public void setStudentDAO(StudentDAO studentDAO) {
this.studentDAO = studentDAO;
}

@Override
public List<Student> getStudents() {
return studentDAO.getStudentList();
}

@Override
public int insertStudent() {
return studentDAO.insertStudent();
}
}

spring配置文件

数据源

druid进入官网,查看配置,网络不可,插眼???

1
2
3
4
5
6
7
8
9
10
<!--声明数据源对象,使用阿里巴巴的druid连接池-->
<bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<!--使用set注入提供配置信息,如果在properties文件中进行看配置,则需要使用${属性名称的方式},十jdbc了解到他的位置
<context:property-placeholder location="jdbc.properties"/>
-->
<property name="url" value="jdbc:mysql://localhost:3306/school"/>
<property name="username" value="root"/>
<property name="password" value="200101"/>
</bean>

mybaits

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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>
<!--设置日志-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!--设置包名-->
<typeAliases>
<!--实体类所在的包名称-->
<package name="com.zss.pojo"/>
</typeAliases>

<!--该包下面的所有包配置文件-->
<mappers>
<package name="com.zss.dao"/>
</mappers>
</configuration>

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
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">

<!--声明数据源对象-->
<bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<!--使用set注入提供配置信息-->
<property name="url" value="jdbc:mysql://localhost:3306/school"/>
<property name="username" value="root"/>
<property name="password" value="200101"/>
</bean>
<!--声明sqlSessionFactoryBean类,创建sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--设置植入数据库的连接池-->
<property name="dataSource" ref="myDataSource"/>
<!--找到mybaits主配置文件的位置,如果要在主配置文件中加入其他文件的配置,需要使用classpath,赋值需要使用value-->
<property name="configLocation" value="classpath:mybaits.xml"/>
</bean>

<!--创建dao,MapperScannerConfigurer这个类在内部调用get-mapper生成每个接口的代理对象,同时会扫描包名,每一个接口执行getMapper的方法,得到dao对象放到容器中,而且我们可以发现这里的dao都是第一个首字母小写-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
为每一个类似studentdao.xml创建一个sqlSessionFactory
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="com.zss.dao"/>
</bean>

<!--声明service-->
<bean id="studentService" class="com.zss.service.impl.StudentServiceImpl">
<property name="studentDAO" ref="studentDAO"/>
</bean>
</beans>

我们尝试输出这些创建的对象,我们可以看到sqlsessionfactory已经被创建出来了

1
2
3
4
5
6
7
8
9
10
11
12
13
public class AppTest 
{
@Test
public void test01(){
String config="applicationContext.xml";
ApplicationContext applicationContext=new ClassPathXmlApplicationContext(config);
String[] names=applicationContext.getBeanDefinitionNames();
for (String name:names){
System.out.println(name);
System.out.println(applicationContext.getBean(name));
}
}
}

image-20220507161304645

测试

1
2
3
4
5
6
7
8
9
10
11
12
//这些方法的提交是自动提交的
@Test
public void test02(){
String config="applicationContext.xml";
ApplicationContext applicationContext=new ClassPathXmlApplicationContext(config);
List<Student> list=new ArrayList<>();
StudentService studentService=(StudentService) applicationContext.getBean("studentService");
list=studentService.getStudents();
for (Student student:list){
System.out.println(student);
}
}

image-20220507163128231