1. 开发流程
- a. 创建实体类
package com.fuyi.entity;
import java.util.Date;
public class User {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
// 此处省略getter和setter
}
- b. 引入Mybatis、Mysql和Juint依赖
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
</dependencies>
- c. 配置映射配置文件
<?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="UserMapper">
<!--查询所有-->
<select id="findAll" resultType="com.fuyi.entity.User">
select * from user
</select>
</mapper>
命名空间(namespace),它的作用主要是利用namespace+id的方式进而确定唯一的语句映射关系
- d. 配置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>
<!--环境配置-->
<environments default="development">
<!--使用MySQL环境-->
<environment id="development">
<!--使用JDBC类型事务管理器-->
<transactionManager type="JDBC"></transactionManager>
<!--使用连接池-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///mybatis_db"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</dataSource>
</environment>
</environments>
<!--加载映射配置-->
<mappers>
<mapper resource="mapper/UserMapper.xml"></mapper>
</mappers>
</configuration>
- e. 编写测试类
@Test
public void test() throws IOException {
// 1. 加载核心配置文件
InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
// 2. 获取SQLFactory工厂对象
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(resourceAsStream);
// 3. 获取SQLSession对象
SqlSession sqlSession = factory.openSession();
// 4. 执行SQL参数
List<User> result = sqlSession.selectList("UserMapper.findAll");
// 5. 打印结果
result.forEach(System.out::println);
// 6. 释放资源
sqlSession.close();
}
2. 增删查改(CURD)
2.1 增加操作
<!-- 新增 -->
<insert id="save" parameterType="com.fuyi.entity.User">
insert into user (username, birthday, sex, address)
value (#{username}, #{birthday}, #{sex}, #{address})
</insert>
- parameterType表示传入的参数类型
- Sql语句中使用#{实体属性名}方式引用实体中的属性值
@Test
public void saveTest() throws IOException {
// 1. 加载核心配置文件
InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
// 2. 获取SQLFactory工厂对象
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(resourceAsStream);
// 3. 获取SQLSession对象
SqlSession sqlSession = factory.openSession();
// 4. 创建user实体对象
User user = new User();
user.setUsername("Lucy");
user.setBirthday(new Date());
user.setSex("女");
user.setAddress("广东广州");
// 5. 执行SQL参数
int insert = sqlSession.insert("UserMapper.save", user);
System.out.println(insert);
// 6. 提交事务
sqlSession.commit();
// 7. 释放资源
sqlSession.close();
}
2.2 更新操作
<!-- 修改 -->
<update id="updateById" parameterType="com.fuyi.entity.User">
update user
set
username = #{username},
birthday = #{birthday},
sex = #{sex},
address = #{address}
where id = #{id}
</update>
@Test
public void updateTest() throws IOException {
// 1. 加载核心配置文件
InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
// 2. 获取SQLFactory工厂对象
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(resourceAsStream);
// 3. 获取SQLSession对象
SqlSession sqlSession = factory.openSession();
// 4. 创建user实体对象
User user = new User();
user.setId(5);
user.setUsername("Lucy");
user.setBirthday(new Date());
user.setSex("女");
user.setAddress("广东肇庆");
// 5. 执行SQL参数
int update = sqlSession.update("UserMapper.updateById", user);
System.out.println(update);
// 6. 提交事务
sqlSession.commit();
// 7. 释放资源
sqlSession.close();
}
2.3 删除操作
<!-- 删除 -->
<delete id="deleteById" parameterType="java.lang.Integer">
delete from user where id = #{id}
</delete>
在这里,SQL语句可以使用#{任意字符串}方式引用传递的单个参数
@Test
public void deleteTest() throws IOException {
// 1. 加载核心配置文件
InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
// 2. 获取SQLFactory工厂对象
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(resourceAsStream);
// 3. 获取SQLSession对象
SqlSession sqlSession = factory.openSession();
// 4. 执行SQL参数
int delete = sqlSession.delete("UserMapper.deleteById", 5);
System.out.println(delete);
// 5. 提交事务
sqlSession.commit();
// 6. 释放资源
sqlSession.close();
}