1.MyBatis的简介
MyBatis是一个持久层框架,可以自定义SQL语句以及配置映射文件,几乎包含了JDBC的代码功能,使用MyBatis可以减少数据取出时的封装 , 数据库的建立连接等重复的代码块,可以通过XML文件或者注解来配置映射原始数据类型、接口和POJO为数据库中的记录
使用MyBatis框架可以直接通过Java代码操作数据库
2.一个简单的MyBatis
2.1工程结构
2.2核心配置文件
<?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="mysql">
<!-- 环境变量-->
<environment id="mysql">
<!-- 配置事务管理器-->
<transactionManager type="JDBC"/>
<!-- 数据源-->
<dataSource type="POOLED">
<!-- 配置数据库连接-->
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/db?serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="******"/>
</dataSource>
</environment>
</environments>
<!--绑定映射文件的位置-->
<mappers>
<mapper resource="com/itcode/dao/Mapper.xml"></mapper>
</mappers>
</configuration>
2.3 Mapper.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必须是某个接口的全限定接口名-->
<mapper namespace="com.itcode.dao.UserMapper">
<!-- id是该接口的某个方法名-->
<!-- resultType是某个类的全限定类名-->
<select id="findAll" resultType="com.itcode.pojo.User">
select * from user;
</select>
<select id="selectById" parameterType="int" resultType="com.itcode.pojo.User">
select * from user where id=#{id};
</select>
<insert id="addUser" parameterType="com.itcode.pojo.User" >
insert into user(id,username,birthday,sex,address) values (#{id},#{username},#{birthday},#{sex},#{address});
</insert>
<update id="updateUser" parameterType="com.itcode.pojo.User">
update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id};
</update>
<delete id="deleteUser">
delete from db.user where id=#{id};
</delete>
</mapper>
2.4 MyBatis的工具类
package com.itcode.utils;
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 java.io.IOException;
import java.io.InputStream;
/*
实现MyBatis的工具类
*/
public class MyBatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
//获取SqlSessionFactory工厂对象
String resource = "SqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//获取SqlSession对象
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
2.5 User
package com.itcode.pojo;
import java.util.Date;
public class User {
private int id;
private String username;
private Date birthday;
private String sex;
private String address;
public User() {
}
public User(int id, String username, Date birthday, String sex, String address) {
this.id = id;
this.username = username;
this.birthday = birthday;
this.sex = sex;
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", birthday=" + birthday +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}';
}
}
2.6 测试类
package com.itcode.dao;
import com.itcode.pojo.User;
import com.itcode.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.Date;
import java.util.List;
public class UserMapperTest {
@Test
public void test01(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> users = mapper.findAll();
for(User user : users){
System.out.println(user);
}
sqlSession.close();
}
@Test
public void selectById(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.selectById(46);
System.out.println(user);
sqlSession.close();
}
@Test
public void addUser(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.addUser(new User(49,"Lisa",new Date(),"女","韩国"));
sqlSession.commit();
sqlSession.close();
}
@Test
public void updateUser(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.updateUser(new User(49,"lili",new Date(),"男","美国"));
sqlSession.commit();
sqlSession.close();
}
@Test
public void deleteUser(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.deleteUser(49);
sqlSession.commit();
sqlSession.close();
}
}