目录
2.5. 定义操作 users 表的 sql 映射文件 userMapper.xml
1、mybatis介绍
MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀==持久层框架==。MyBatis 消除了几乎所有的 JDBC 代码和参数的手工设置以及对结果集的检索封装。MyBatis 可以使用简单的 XML 或注解用于配置和原始映射,将接口和 Java 的 POJO(Plain Old Java Objects,普通的 Java 对象)映射成数据库中的记录. 半自动化框架。必须写sql语句。
2、mybatis快速入门
2.1添加jar包
【mybatis】
mybatis-3.1.1.jar
【MYSQL 驱动包】
mysql-connector-java-5.1.7-bin.jar【log4j】
【lombok】
【junit】
2.2. 建库+表
create database mybatis;
use mybatis;
CREATE TABLE users(id INT PRIMARY KEY AUTO_INCREMENT, NAME
VARCHAR(20), age INT);
INSERT INTO users(NAME, age) VALUES('Tom', 12);
INSERT INTO users(NAME, age) VALUES('Jack', 11);
2.3添加 Mybatis 的配置文件conf.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 type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=Asia/Shanghai" />这里是你自己的数据库名称
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
</configuration>
2.4. 定义表所对应的实体类
@Data(这里可以使用lombok插件,简化开发)
public class User {
private int id;
private String name;
private int age;
//get,set 方法
}
2.5. 定义操作 users 表的 sql 映射文件 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="demo01">
<select id="getUser" parameterType="int" resultType="pojo.User">
select * from tb_user where u_id=#{id}
</select>
</mapper>
注意:==把映射文件注册到配置文件上== mybatis.xml上
<mappers>
<mapper resource="mapper/userMapper.xml"/>
</mappers>
完整的crud
<?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="selectOne" parameterType="int" resultType="com.lwh.pojo.User" resultMap="UserResult">
select * from tb_user where u_id=#{id}
</select>
<update id="updateOne" parameterType="com.lwh.pojo.User" >
update tb_user set u_name=#{name} where u_id=#{id}
</update>
<select id="selectAll" resultType="com.lwh.pojo.User" resultMap="UserResult">
select * from tb_user
</select>
<insert id="insertOne" parameterType="com.lwh.pojo.User" parameterMap="com.lwh.pojo.User">
insert into tb_user values(null,u_name=#{name},realname=#{realname})
</insert>
<delete id="deleteOne" parameterType="int">
delete from tb_user where u_id=#{id}
</delete>
<resultMap id="UserResult" type="com.lwh.pojo.User">
<result property="id" column="u_id"/>
<result property="name" column="u_name"/>
</resultMap>
</mapper>
2.6测试代码
import com.lwh.pojo.User;
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 org.junit.Test;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
/**
* @创建人 xiaoliu
* @创建时间 2022/5/31
* @描述
*/
public class Demo {
@Test
public void demoSelectOne() throws Exception {
Reader reader = Resources.getResourceAsReader("mybaits.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = factory.openSession();
User user = session.selectOne("usermapper.selectOne", 1);
System.out.println(user);
}
@Test
public void demoUpdateOne() throws Exception{
Reader reader = Resources.getResourceAsReader("mybaits.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
SqlSession sqlSession = factory.openSession();
User user = new User();
user.setName("大帅哥");
user.setId(1);
int i = sqlSession.update("usermapper.updateOne", user);
sqlSession.commit();
sqlSession.close();
System.out.println(i);
}
@Test
public void insertOne() throws Exception{
Reader reader = Resources.getResourceAsReader("mybaits.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = factory.openSession();
User user = new User();
user.setRealname("lwh");
user.setName("lwh");
int insert = session.insert("usermapper.insertOne", user);
session.commit();
session.close();
System.out.println(insert);
}
@Test
public void deleteOne() throws Exception{
Reader reader = Resources.getResourceAsReader("mybaits.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = factory.openSession();
int delete = session.delete("usermapper.deleteOne", 1);
session.commit();
session.close();
System.out.println(delete);
}
@Test
public void selectAll() throws Exception{
Reader reader = Resources.getResourceAsReader("mybaits.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = factory.openSession();
List<User> user = session.selectList("usermapper.selectAll");
for (User user1 : user) {
System.out.println(user1);
}
}
}