MyBatis入门
1.MyBatis开发的步骤:
- 添加MyBatis坐标
- 创建User数据表
- 创建User实体类
- 编写映射文件UserMapper.xml文件
- 编写核心文件SqlMapConfig文件
- 编写测试类
1.添加MyBatis坐标
<!--添加MyBatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!--添加数据库坐标-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
<!--由于测试需要添加junit依赖-->
<dependency>
<groupId>org.apache.isis.viewer</groupId>
<artifactId>junit</artifactId>
<version>0.1.2-incubating</version>
</dependency>
<!--打印日记添加坐标-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
2.第二步:创建数据表(包含字段id、username、password)
CREATE TABLE IF NOT EXISTS USER(
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(52) NOT NULL,
PASSWORD VARCHAR(52) NOT NULL
);
INSERT INTO USER VALUES(NULL,'海康','123'),
(NULL,'湛江','123'),(NULL,'南宁','123');
SELECT * FROM USER;
3.第三步:创建User实体
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class User{
private int id;
private String username;
private String password;
}
4.第四步:编写映射文件UserMapper.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">
<mapper namespace="userMapper">
表示查询全部
<select id="findAll" resultType="com.haikang.pojo.User">
select * from user
</select>
</mapper>
5.第五步:编写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">
<environment id="development">
transactionManager:事务布局器
<transactionManager type="JDBC"></transactionManager>
<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="root"/>
</dataSource>
</environment>
</environments>
<!-- 由于MyBatis提供的api只能读取一个配置所有需要引入UserMapper.xml配置文件-->
<mappers>
<mapper resource="com/haikang/mapper/UserMapper.xml"/>
</mappers>
</configuration>
mapper resource=“com/haikang/mapper/UserMapper.xml”
注意是“/”
6.测试类编写
- 加载配置文件:Resources.getResourceStream(“加载文件名”);
- 获得构sqlSession工厂对象:SqlSessionFactoryBuilder().build(“加载文件返回值”);
- 获得sqlSession对象
- 执行Sql语句
- 打印数据
- 关流
//加载配置文件
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
//获得sqlSession工厂对象
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
//获得SqlSession对象
SqlSession sqlSession = build.openSession();
//执行Sql语句
List<User> user = sqlSession.selectList("userMapper.findAll");
for (User users:
user) {
System.out.println(users);
}
sqlSession.close();
}
MyBatis的映射文件概述:
2.MyBatis的插入数据操作
步骤:
1.编写UserMapper映射文件
<!--添加数据-->
<insert id="add" parameterType="com.haikang.pojo.User">
insert into user (id,username,password) values (#{id},#{username},#{password})
</insert>
2.编写测试类:注意是由于MyBatis默认是不提交事务的,必须手动提交事务
@Test
public void test2() throws IOException {
//加载配置文件
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
//获得SqlSession工厂对象
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
//获得SqlSession对象
SqlSession sqlSession = build.openSession();
//执行Sql语句
int insert = sqlSession.insert("userMapper.add", new User(5, "海康", "123456"));
System.out.println(insert);
//注意是:由于MyBatis默认是不提交事务的,不像JDBC
sqlSession.commit();
//关流
sqlSession.close();
}
MyBatis的插入数据操作的注意问题:
- 插入语句使用insert标签
- 在映射 文件中使用parameterType属性指定要插入的数据类型
- Sql语句中使用#{实体属性名}方式引用实体中的属性值
- 插入操作使用API是sqlSession.insert(“命名空间.id”,实体对象);
- 插入操作涉及的数据库数据变化,所以使用sqlSession对象显示的提交事务,即sqlSession.commit()
3.MyBatis的修改数据操作
MyBatis的修改数据操作步骤:
1.编写UserMapper.xml文件
2.编写测试类
<!--修改数据-->
<update id="update" parameterType="com.haikang.pojo.User">
update user set username=#{username} where id=#{id}
</update>
@Test
public void test4() throws IOException {
User user = new User();
user.setId(2);
//加载配
置文件
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
//获得SqlSession工厂对象
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
//获得SqlSession对象
SqlSession sqlSession = build.openSession();
//执行sql语句
int delete = sqlSession.delete("userMapper.delete", user);
System.out.println(delete);
//提交事务
sqlSession.commit();
//关流
sqlSession.close();
}
MyBatis的修改数据操作注意点:
- 修改语句使用update标签
- 修改操作使用API是SqlSession.update(“命名空间.id”,实体对象)
4.MyBatis删除数据操作
步骤:
- 编写UserMapper.xml文件
- 编写测试类
<!--删除数据,对应的测试是Test4-->
<delete id="delete" parameterType="com.haikang.pojo.User">
delete from user where id=#{id}
</delete>
<!--删除数据的别种方式,只根据id删除,对应的测试是test5-->
<delete id="delect2" parameterType="java.lang.Integer">
delete from user where id=#{id}
</delete>
@Test
public void test4() throws IOException {
User user = new User();
user.setId(2);
//加载配置文件
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
//获得SqlSession工厂对象
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
//获得SqlSession对象
SqlSession sqlSession = build.openSession();
//执行sql语句
int delete = sqlSession.delete("userMapper.delete", user);
System.out.println(delete);
//提交事务
sqlSession.commit();
//关流
sqlSession.close();
}
@Test
public void test5() throws IOException {
//加载配置文件
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
//获得SqlSess工厂对象
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
//获得SqlSess对象
SqlSession sqlSession = build.openSession();
//执行Sql语句
int delete = sqlSession.delete("userMapper.delete", 5);
System.out.println(delete);
//提交事务
sqlSession.commit();
//关流
sqlSession.close();
}