一.MyBatis
入门
1.ORM
对象关系映射。
指的是持久化数据和实体对象的映射关系,为了解决面向对象和关系型数据库存在的互不匹配的现象的技术
一个表就是一个类,表里面的一条数据就是类的一个对象。
2.MyBatis
基本操作
2.1 导入jar
包
mysql-connetor-8.0.33-bin.jar
mybati-3.5.13.jar
log4j.1.2.17.jar
2.2 创建实体类(字段和数据库对应的表一致)
package com.lzh.model;
import lombok.Data;
@Data
public class UserModel {
private int id;
private String username;
private String password;
private String email;
}
2.3编写持久层接口和实现类
持久层接口:提供方法
实现类:实现接口,重写方法,进行进一步操作
package com.lzh.dao;
import com.lzh.model.UserModel;
import java.util.List;
public interface UserDao {
List<UserModel> selectAll();
}
package com.lzh.dao.impl;
import com.lzh.dao.UserDao;
import com.lzh.model.UserModel;
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;
import java.util.List;
public class UserDaoImpl implements UserDao {
@Override
public List<UserModel> selectAll() {
InputStream in;
try {
//1.加载配置问件
in = Resources.getResourceAsStream("mybatis-config.xml");
//2.创建SqlSessionFactory构建者对象
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
//3.创建SqlSessionFatory对象
SqlSessionFactory factory = builder.build(in);
//4.创建SqlSession对象
SqlSession sqlSession = factory.openSession();
//5.通过SqlSession对象执行sql,其参数是配置文件里面的namespace+id
List<UserModel> list = sqlSession.selectList("com.lzh.dao.UserDao.java.selectAll");
return list;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
2.4编写持久层接口的映射文件(文件名.xml)
文件名必须以持久层接口名称命名
同一个文件里不能出现相同的id
namespace="持久层接口的全限定名" //(mapper属性)
id="对应方法名" //(当然也可以随便起)(select属性)
resultType="要映射的实体类的全限定名" //(select属性) 查询的时候是这个属性 增改的时候是paramType
<?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="com.lzh.dao.UserDao">
<select id="selectAll" resultType="com.lzh.model.UserModel">
select* from user;
</select>
</mapper>
2.5编写mybatis-config.xml
这个文件的名字可以随便取,主要是为了提供mysql
的各种配置
<?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>
<!--配置mybatis的环境-->
<environments default="mysql">
<!--配置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/manager"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!--告知mybatis映射配置的位置-->
<mappers>
<mapper resource="UserDao.xml"/>
<mapper resource="StudentInfoDao.xml"/>
</mappers>
</configuration>
二.不写实现类如何使用MyBatis
由于每个接口都要有一个实现类,这样比较繁琐,那么有没有好的办法简化开发?
使用接口代理对象
1 导入jar
包
2.创建实体类(字段和数据库对应的表一致)
package com.lzh.model;
import lombok.Data;
@Data
public class StudentInfo {
private int id;
private int age;
private String name;
private double score;
}
3.编写service层
创建空参构造用于加载配置文件等操作和提供方法以及获取接口代理对象(这个步骤可以写在测试类的@Before,关闭资源写在测试类的@After)
记得设置自动提交事务和关闭资源
package com.lzh.service;
import com.lzh.dao.StudentInfoDao;
import com.lzh.model.StudentInfo;
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;
import java.util.List;
public class StudentInfoService {
StudentInfoDao dao;
InputStream in;
SqlSession sqlSession;
SqlSessionFactory factory;
public StudentInfoService() {
try {
in = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
factory = builder.build(in);
sqlSession = factory.openSession();
dao = sqlSession.getMapper(StudentInfoDao.class);
} catch (IOException e) {
e.printStackTrace();
}
}
public void commit() {
try {
sqlSession.commit();
sqlSession.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public List<StudentInfo> findAll() {
return dao.selectAll();
}
public List<StudentInfo> Age(int age) {
return dao.selectAge(age);
}
public int update(StudentInfo studentInfo) {
return dao.updateStudent(studentInfo);
}
public int insert(StudentInfo studentInfo){
return dao.insertStudent(studentInfo);
}
public int delete(int id){
return dao.deleteStudent(id);
}
}
三.如何在MyBatis
中传入值?
3.1传入一个值
int deleteStudent( int id);
<delete id="deleteStudent">
delete from studentinfo where id=#{id};//或者 delete from studentinfo where id=#{values}
</delete>
#{id}此时里面写的变量要和传入的那个变量名称保持一致
#{values}直接写这个
3.2 传入多个值
可以使用对象作为参数#{对象属性名}
int updateStudent(StudentInfo studentInfo);//持久化接口
public int update(StudentInfo studentInfo) {//service
return dao.updateStudent(studentInfo);
}
<update id="updateStudent" parameterType="com.lzh.model.StudentInfo">
update studentinfo set score=#{score} where id=#{id};
</update>
@Test
public void studentUpdate() throws IOException {//测试类
studentInfo.setId(1);
studentInfo.setScore(87.0);
int i = studentInfoService.update(studentInfo);
studentInfoService.commit();
System.out.println(i);
}
直接传入参数
如果有多个参数
#{arg0}
#{arg1}
或者
#{param1}
#{param2}
Student selectByNameAndGender(String name, String gender);
<select id="selectByNameAndGender" resultType="com.kfm.model.Student">
select * from student where name = #{param1} and gender = #{arg1};
</select>
@Test
public void selectByNameAndGender() {
Student stu = studentDao.selectByNameAndGender("张一健", "男");
System.out.println(stu);
}