一、mybatis的详解
1、mybatis配置
SqlMapConfig.xml,此文件作为mybatis的全局配置文件,配置了mybatis的运行环境等信息。
mapper.xml文件即sql映射文件,文件中配置了操作数据库的sql语句。此文件需要在SqlMapConfig.xml中加载。
2、通过mybatis环境等配置信息构造SqlSessionFactory即会话工厂
3、由会话工厂创建sqlSession即会话,操作数据库需要通过sqlSession进行。
4、mybatis底层自定义了Executor执行器接口操作数据库,Executor接口有两个实现,一个是基本执行器、一个是缓存执行器。
5、Mapped Statement也是mybatis一个底层封装对象,它包装了mybatis配置信息及sql映射信息等。mapper.xml文件中一个sql对应一个Mapped Statement对象,sql的id即是Mapped statement的id。
6、Mapped Statement对sql执行输入参数进行定义,包括HashMap、基本类型、pojo,Executor通过Mapped Statement在执行sql前将输入的java对象映射至sql中,输入参数映射就是jdbc编程中对preparedStatement设置参数。
7、Mapped Statement对sql执行输出结果进行定义,包括HashMap、基本类型、pojo,Executor通过Mapped Statement在执行sql后将输出结果映射至java对象中,输出结果映射过程相当于jdbc编程中对结果的解析处理过程。
二、业务需求
使用MyBatis实现以下功能:
根据用户id查询一个用户
根据用户名称模糊查询用户列表
添加用户
更新用户
删除用户
三、创建java工程
package com.itheima;
public class Student {
private Integer id;
private String name;
private Integer age;
private String sex;
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
public String getSex() {
return sex;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + "]";
}
}
四、加入核心包
mybatis依赖包
数据库驱动包(已添加)
效果:
五、创建 SqlMapConfig.xml
<?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>
<!-- 和spring整合后 environments配置将废除 -->
<environments default="development">
<environment id="development">
<!-- 使用jdbc事务管理 -->
<transactionManager type="JDBC" />
<!-- 数据库连接池 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="sql/student.xml"/>
</mappers>
</configuration>
SqlMapConfig.xml是mybatis核心配置文件,配置文件内容为数据源、事务管理。
六、5.3.4.创建pojo
pojo类作为mybatis进行sql映射使用,po类通常与数据库表对应
DROP TABLE IF EXISTS student
;
CREATE TABLE student
(
id
int(11) NOT NULL AUTO_INCREMENT,
name
varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
age
int(11) NULL DEFAULT NULL,
sex
varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (id
) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 10 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
七、sql映射文件
User.xml映射文件
<?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">
<!-- namespace:命名空间,用于隔离sql,还有一个很重要的作用,后面会讲 -->
//namespace:命名空间
//parameterType:参数类型
//resultType:返回类型(要写类全名)
<mapper namespace="test">
<select id="map1" parameterType="Integer" resultType="com.itheima.Student">
<!-- 写sql的语句 -->
//这里的 id=#{a}<==>等价于id=?是一个占位符号
select * from student where id=#{a}
</select>
<!--改变用户名 -->
<update id="map2" parameterType="com.itheima.Student">
update student set name=#{name},age=#{age},sex=#{sex}
where id=#{id}
</update>
<!--删除用户名 -->
<delete id="map3" parameterType="Integer">
delete from student where id=#{cs}
</delete>
<!-- 增加用户 -->
<insert id="map4" parameterType="com.itheima.Student">
insert into student(id,name,age,sex) values(#{id},#{name},#{age},#{sex})
</insert>
<!-- 查询多个用户 -->
<select id="map5" parameterType="String" resultType="com.itheima.Student">
select * from student where name=#{ade}
</select>
<!--模糊查询 -->
<select id="map6" parameterType="String" resultType="com.itheima.Student">
<!-- select * from student where name like '%${sf}%' -->
SELECT * FROM Student WHERE name like '%${value}%'
</select>
</mapper>
八、加载映射文件
九、测试程序
package com.itheima;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
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;
public class Juntis {
@Test
//用id查询用户
public void test1() throws IOException {
//1.加载配置文件
String a = "SqlMapConfig.xml";
//2.获得输入流
InputStream resourceAsStream = Resources.getResourceAsStream(a);
//3. 创建SqlSessionFactory对象
SqlSessionFactory build = new
SqlSessionFactoryBuilder().build(resourceAsStream);
// 4. 创建SqlSession对象
SqlSession openSession = build.openSession();
Student selectOne = openSession.selectOne("test.map1", 1);
System.out.println(selectOne);
}
@Test
//改变用户名
public void test2() throws IOException {
//1.加载配置文件
String a = "SqlMapConfig.xml";
//2.获得输入流
InputStream resourceAsStream = Resources.getResourceAsStream(a);
//3.
SqlSessionFactory build = new
SqlSessionFactoryBuilder().build(resourceAsStream);
//4.
SqlSession openSession = build.openSession();
Student s = new Student();
s.setId(1);
s.setName("12");
s.setAge(10);
s.setSex("男");
int update = openSession.update("test.map2",s);
openSession.commit();
System.out.println(update);
}
@Test
//删除
public void test3() throws IOException {
//1.加载配置文件
String a = "SqlMapConfig.xml";
//2.获得输入流
InputStream resourceAsStream = Resources.getResourceAsStream(a);
//3.
SqlSessionFactory build = new
SqlSessionFactoryBuilder().build(resourceAsStream);
//4.
SqlSession openSession = build.openSession();
int update = openSession.delete("test.map3",4);
openSession.commit();
System.out.println(update);
}
//增加用户
@Test
//删除
public void test4() throws IOException {
//1.加载配置文件
String a = "SqlMapConfig.xml";
//2.获得输入流
InputStream resourceAsStream = Resources.getResourceAsStream(a);
//3.
SqlSessionFactory build = new
SqlSessionFactoryBuilder().build(resourceAsStream);
//4.
SqlSession openSession = build.openSession();
Student s1 = new Student();
s1.setName("吴九");
s1.setAge(59);
s1.setSex("男");
int insert = openSession.insert("test.map4",s1);
openSession.commit();
System.out.println(insert);
}
@Test
//查询全部应用
public void test5() throws IOException {
//1.加载配置文件
String a = "SqlMapConfig.xml";
//2.获得输入流
InputStream resourceAsStream = Resources.getResourceAsStream(a);
//3.
SqlSessionFactory build = new
SqlSessionFactoryBuilder().build(resourceAsStream);
//4.
SqlSession openSession = build.openSession();
List<Student> selectList = openSession.selectList("test.map5","吴九");
openSession.commit();
System.out.println(selectList);
}
@Test
//模糊查询
public void test6() throws IOException {
//1.加载配置文件
String a = "SqlMapConfig.xml";
//2.获得输入流
InputStream resourceAsStream = Resources.getResourceAsStream(a);
//3.
SqlSessionFactory build = new
SqlSessionFactoryBuilder().build(resourceAsStream);
//4.
SqlSession openSession = build.openSession();
List<Student> selectList = openSession.selectList("test.map6","吴");
openSession.commit();
System.out.println(selectList);
}
}