Mybatis 学习
MyBatis 基础
主流的 ORM 框架,之前叫 iBatis, 后来改名 MyBatis
如果你使用的是 Spring MyBatis 而不是 Spring Boot,并希望自动将实体类生成数据库表(类似于 JPA 的自动表生成),MyBatis 本身并不支持这种功能。MyBatis 是一个 ORM 框架,用于映射 SQL 语句和 Java 对象,但它不会像 JPA 那样自动创建数据库表。
不能自动创建数据库表,不支持!
是的,MyBatis 本身只能使用已经存在的数据库和表结构,它不会像 JPA 或 Hibernate 那样自动生成数据库表结构。
可以 CRUD
MyBatis 入门
1,配置 pom.xml
<dependencies>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.16</version>
</dependency>
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
</dependency>
<!-- ehcache -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.11</version>
</dependency>
<!-- ehcache -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-ehcache</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
2,创建实体类
package com.xs.entity;
import lombok.Data;
@Data
public class People {
private Integer id;
private String name;
private Double money;
}
3,配置 config.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>
<!-- 配置 MyBatis 运行 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatistest?useUnicode=true&charset=UTF-8"/> <!-- 修改你自己的database 名称 -->
<property name="username" value="root"/>
<property name="password" value=""/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 修改为 resources 根目录的映射文件 -->
<mapper resource="mapper/PeopleMapper.xml"/> <!-- 这里的路径要与实际文件位置一致 -->
</mappers>
</configuration>
4,配置 resource/mapper/PeopleMapper.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="PeopleMapper">
<select id="findById" parameterType="java.lang.Integer" resultType="com.xs.entity.People">
select * from people where id = #{id}
</select>
</mapper>
5,Mysql 里创建一个数据
6,main
package com.xs.test;
import com.xs.entity.People;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.InputStream;
public class Test {
public static void main(String[] args) {
// 加载 MyBatis 配置文件
InputStream inp = Test.class.getClassLoader().getResourceAsStream("config.xml");
SqlSessionFactoryBuilder sqlSFB = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlF = sqlSFB.build(inp);
// 获取 SqlSession
SqlSession sqlS = sqlF.openSession();
String statement = "PeopleMapper.findById"; // 匹配的 namespace 和 id
People people = sqlS.selectOne(statement,1);
System.out.println(people);
sqlS.close();
}
}
Mapper 代理实现自定义接口
使用这个方式 , 比较熟悉
1,自定义 接口
package com.xs.repository;
import com.xs.entity.People;
import java.util.List;
public interface PeopleRepository {
public int save(People people);
public int deleteById(Integer id);
public int update(People people);
public People findById(Integer id);
public List<People> findAll();
// public People findById2(int id);
// public People findByIdAndName(int id, String name);
}
2,配置 resource/repository/PeopleRepository
<?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.xs.repository.PeopleRepository">
<insert id="save" parameterType="com.xs.entity.People">
insert into people(name, money) values(#{name},#{money})
</insert>
<delete id="deleteById" parameterType="java.lang.Integer">
delete from people where id = #{id}
</delete>
<update id="update" parameterType="com.xs.entity.People">
update people set name = #{name}, money = #{money} where id = #{id}
</update>
<select id="findById" parameterType="java.lang.Integer" resultType="com.xs.entity.People">
select * from people where id = #{id}
</select>
<select id="findAll" resultType="com.xs.entity.People">
select * from people
</select>
<!-- <select id="findById2" parameterType="int" resultType="com.xs.entity.People">-->
<!-- select * from people where id = #{num}-->
<!-- </select>-->
</mapper>
3,配置 config.xml
<mappers>
<!-- 修改为 resources 根目录的映射文件 -->
<mapper resource="repository/PeopleRepository.xml"/> <!-- xml的路径 -->
</mappers>
4,main
package com.xs.test;
import com.xs.entity.People;
import com.xs.repository.PeopleRepository;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.InputStream;
public class Test2 {
public static void main(String[] args) {
// 加载 MyBatis 配置文件
InputStream inp = Test2.class.getClassLoader().getResourceAsStream("config.xml");
SqlSessionFactoryBuilder sqlSFB = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlF = sqlSFB.build(inp);
// 获取 SqlSession
SqlSession sqlS = sqlF.openSession();
PeopleRepository peopleRepository = sqlS.getMapper(PeopleRepository.class);
// add & save
// People p = new People();
// p.setName("July");
// p.setMoney(Double.parseDouble("657"));
// peopleRepository.save(p);
// update
// People p = peopleRepository.findById(2);
// p.setName("哈哈");
// p.setMoney(Double.parseDouble("111"));
// peopleRepository.update(p);
// findAll
// List<People> list = peopleRepository.findAll();
// for(People people:list){
// System.out.println(people);
// }
sqlS.commit(); // 不要忘记提交事务
sqlS.close();
}
}