Mybatis
第一步-编写工具类
package com.ruoxue.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;//sqlSessionFactory --> sqlSession
public class MybatisUtils {private static SqlSessionFactory sqlSessionFactory;static {try {//使用Mybatis获取sqlSessionFactory对象String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);} catch (IOException e) {e.printStackTrace();}}public static SqlSession getSqlSession() {return sqlSessionFactory.openSession();}
}
第二步-mybatis-config配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""https://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/mybatis?userSSL=true& useUniCOde=true& characterEncoding=UTF-8"/><property name="username" value="root"/><property name="password" value="123456"/></dataSource></environment></environments><mappers><mapper resource="com/biruoxue/dao/UserMapper.xml"/></mappers>
</configuration>
第三步-编写实体类
第四步-接口
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace=绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.biruoxue.dao.UserDao"><!--查询语句--><select id="getUserList" resultType="com.biruoxue.pojo.User">select * from mybatis.user</select>
</mapper>
第五步-测试方法
package com.ruoxue.dao;import com.biruoxue.pojo.User;
import com.biruoxue.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.jupiter.api.Test;import java.util.List;public class UserDaoTest {@Testpublic void test(){// 获得sqlSession对象SqlSession sqlSession = MybatisUtils.getSqlSession();//方式一:getMapper,执行SQLUserDao userDao = sqlSession.getMapper(UserDao.class);List<User> userList = userDao.getUserList();for(User user : userList){System.out.println(user);}sqlSession.close();}}
CRUD操作
namespace中包名要和Dao/Mapper中接口的包名一致
id:命名空间中唯一的标识符;接口中的方法名与映射文件中的SQL语句ID 一一对应
parameterType:传入SQL语句的参数类型 。
resultType:SQL语句返回值类型。【完整的类名或者别名】
增删改需提交事务
1.编写接口
package com.biruoxue.dao;import com.biruoxue.pojo.User;import java.util.List;public interface UserDao {//查询全部用户List<User> getUserList();//根据id查询用户User getUserById(int id);//插入int addUser(User user);//修改int updateUser(User user);//删除int deleteUser(int id);
}
2.编写对应mapper中sql语句
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace=绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.biruoxue.dao.UserDao"><!--查询语句--><select id="getUserList" resultType="com.biruoxue.pojo.User">select * from mybatis.user;</select><select id="getUserById" parameterType="int" resultType="com.biruoxue.pojo.User">select * from mybatis.user where id = #{id};</select><insert id="addUser" parameterType="com.biruoxue.pojo.User">insert into mybatis.user (id, name, pwd) values(#{id}, #{name}, #{pwd});</insert><update id="updateUser" parameterType="com.biruoxue.pojo.User">update mybatis.user set name=#{name}, pwd=#{pwd} where id=#{id};</update><delete id="deleteUser" parameterType="int">delete from mybatis.user where id=#{id};</delete>
</mapper>
当实体类字段或参数过多,应当考虑使用Map
Map传递参数,直接在sql中取出key的属性即可
模糊查询
<!--模糊查询--><select id="getUserLike" resultType="com.biruoxue.pojo.User">select * from mybatis.user where name like"%"#{value}"%"</select>