文章目录
MyBatis
1 入门
1.1 创建Maven工程
1.2 导入依赖
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
1.3 编写MyBatis核心配置文件
<?xml version="1.0" encoding="UTF8"?>
<!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/bookSystem_db?serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/pengjian/dao/UserMapper.xml"/>
</mappers>
</configuration>
1.4 创建Util类
package com.pengjian.util;
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;
public class MybatisUtil {
private static SqlSessionFactory sqlSessionFactory;
static {
String resource = "mybatis-config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
public static SqlSession getSqlSession() {
return sqlSessionFactory.openSession();
}
}
1.5 创建Dao层接口
package com.pengjian.dao;
import com.pengjian.entity.User;
import java.util.List;
public interface UserMapper {
List<User> getUserList();
}
1.6 创建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">
<mapper namespace="com.pengjian.dao.UserMapper">
<resultMap id="BookMap" type="com.pengjian.entity.User">
<id property="bookId" column="book_id"></id>
<id property="bookName" column="book_name"></id>
<id property="price" column="price"></id>
</resultMap>
<select id="getUserList" resultMap="BookMap">
select book_id,book_name,price from t_book;
</select>
</mapper>
1.7 编写测试代码
package com.pengjian.dao;
import com.pengjian.entity.User;
import com.pengjian.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class UserDaoTest {
@Test
public void testSelect(){
SqlSession sqlSession = MybatisUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = mapper.getUserList();
for (User user:userList
) {
System.out.println(user);
}
sqlSession.close();
}
}
IDEA设置提示数据表字段名
IDEA自动添加pojo类
MyBatis中Java变量与数据库字段的映射关系
Java中使用驼峰法命名,MySQL中使用下划线法命名,MyBatis要求Java变量名和数据库字段名一直才能将数据库记录封装到Java对象中,这个时候从规则上出现了冲突。解决方法是在xml文件中保存属性名与类成员变量名的对应关系。
<mapper namespace="com.pengjian.mapper.BookMapper">
<resultMap id="BookResultMap" type="com.pengjian.pojo.Book">
<id column="book_id" property="bookId"></id>
<result column="book_name" property="bookName"></result>
<result column="price" property="price"></result>
</resultMap>
<select id="selectBooks" resultMap="BookResultMap">
select * from t_book;
</select>
</mapper>
MyBatis中插入没有主键的对象后(数据库中自动升序)返回有主键的对象
在写插入语句时设置需要返回的类成员变量名,数据库插入数据后会自动将插入的数据(包含自动升序的属性)返回给Java对象
<insert id="insertUser" useGeneratedKeys="true" keyProperty="userId">
insert into t_user(user_name, user_gender) values (#{userName},#{userGender})
</insert>
Java对象
<insert id="insertUser" useGeneratedKeys="true" keyProperty="userId">
insert into t_user(user_name, user_gender) values (#{userName},#{userGender})
</insert>