MyBatis-简单运行

MyBatis的简单运行

全局配置文件

配置文件与映射文件存在于根目录

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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>

<!-- 将写好的sql映射文件(StudentMapper.xml)一定要注册到全局配置文件(mybatis-config.xml)中 -->
<mappers>
<mapper resource="StudentMapper.xml" />
</mappers>
</configuration>

映射文件

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
<?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="Dao.StudentDao">
<!--
namespace:名称空间;这里指定为接口的全类名
id:唯一标识,使用接口的方法名称
resultType:返回值类型
#{id}:从传递过来的参数中取出id值
-->

<!-- public Student getStudentId(Integer id); -->
<select id="getStudentId" resultType="student.Student">
select * from user where id = #{id}
</select>

<!-- public void updateStudent(Integer id,String Name,String sex);
public void updateStudent(@Param("id")Integer id,@Param("name")String Name,@Param("sex")String sex);
-->
<update id="updateStudent">
<!-- 当有多个参数时无法使用属性名来进行绑定,可以使用param1.....paramN 来进行绑定
update user set name=#{param2},sex=#{param3} where id=#{param1} -->
<!-- 如要使用属性名绑定使用@param("")来进行绑定 -->
update user set name=#{name},sex=#{sex} where id=#{id}
</update>

<!-- public void addStudent(Student student); -->
<insert id="addStudent">
insert into user(name,sex) values(#{name},#{sex})
</insert>

<!-- public void deleteStudent(Integer id); -->
<delete id="deleteStudent">
delete from user where id=#{id}
</delete>
</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
38
39
40
41
package student;

public class Student {
private Integer Id;
private String Name;
private String Sex;

public Integer getId() {
return Id;
}
public void setId(Integer id) {
Id = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getSex() {
return Sex;
}
public void setSex(String sex) {
Sex = sex;
}
@Override
public String toString() {
return "Student [Id=" + Id + ", Name=" + Name + ", Sex=" + Sex + "]";
}
public Student(String name, String sex) {
super();
Name = name;
Sex = sex;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}


}

接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package Dao;

import org.apache.ibatis.annotations.Param;

import student.Student;

public interface StudentDao {

public Student getStudentId(Integer id);

public void addStudent(Student student);

public void updateStudent(@Param("id")Integer id,@Param("name")String Name,@Param("sex")String sex);

public void deleteStudent(Integer id);
}

测试类

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
package student.mybatis;

import java.io.IOException;
import java.io.InputStream;

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.jupiter.api.Test;

import Dao.StudentDao;
import student.Student;

class MyBatis_Test {

public SqlSessionFactory getSqlSessionFactory() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
return new SqlSessionFactoryBuilder().build(inputStream);
}
/**
* 1、根据xml配置文件(全局配置文件)创建一个SqlSessionFactory对象 有数据源一些运行环境信息
* 2、sql映射文件;配置了每一个sql,以及sql的封装规则等。
* 3、将sql映射文件注册在全局配置文件中
* 4、写代码:
* 1)、根据全局配置文件得到SqlSessionFactory;
* 2)、使用sqlSession工厂,获取到sqlSession对象使用他来执行增删改查
* 一个sqlSession就是代表和数据库的一次会话,用完关闭
* 3)、使用sql的唯一标志来告诉MyBatis执行哪个sql。sql都是保存在sql映射文件中的。
*
* @throws IOException
*/
//基础方式
@Test
public void test() throws Exception {
// 2、获取sqlSession实例,能直接执行已经映射的sql语句
// sql的唯一标识:statement Unique identifier matching the statement to use.
// 执行sql要用的参数:parameter A parameter object to pass to the statement.
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();

SqlSession openSession = sqlSessionFactory.openSession();
try {
//命名空间+id,这里没使用接口绑定,StudentMapper 随便起的命名空间,getStudentById id名称
Student student = openSession.selectOne("StudentMapper.getStudentById", 1);
System.out.println(student);
} finally {
openSession.close();
}
}
//推荐使用这种方法
@Test
public void test1() throws IOException {
//1.获取SqlSessionFactory
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();

//2.获取sqlSession
SqlSession openSession = sqlSessionFactory.openSession();
//3.获取接口实现类对象
//为接口自动创建一个代理对象,代理对象执行增删改查
try {
StudentDao studentDao = openSession.getMapper(StudentDao.class);
//4.调用方法
Student student =studentDao.getStudentId(1);
System.out.println(student);
}finally {
openSession.close();
}
}

@Test
public void test3() throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession openSession = sqlSessionFactory.openSession();
StudentDao studentDao = openSession.getMapper(StudentDao.class);
//Student student = new Student("李四","男");
try {
//添加方法
//studentDao.addStudent(student);
//删除方法
//studentDao.deleteStudent(6);
//修改方法
studentDao.updateStudent(7,"李四", "男");
//手动提交 可以设置sqlSessionFactory.openSession(true)
openSession.commit();
}finally {
openSession.close();
}
}

}
原创技术分享,您的支持将鼓励我继续创作
0%