MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。
MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。
MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,
将接口和 Java 的 POJOs(Plain Ordinary Java Object,普通的 Java对象)
映射成数据库中的记录
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 父工程-->
<groupId>org.example</groupId>
<artifactId>mybatis</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<!-- 子工程-->
<modules>
<module>mybatis-01</module>
</modules>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
<!-- 数据库驱动-->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<!-- 单元测试-->
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
(1)核心配置文件mybatis-config.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties"/>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC">
<property name="..." value="..."/>
</transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="chen/dao/PersonMapper.xml"/>
</mappers>
</configuration>
(2)db.properties
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/wjl?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
username=root
password=123456
package com.chen.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{
//获取sqlSessionFactory对象
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//获取sqlSession实例
public static SqlSession getSqlSession(){
SqlSession sqlSession = sqlSessionFactory.openSession();
return sqlSession;
}
}
package com.chen.pojo;
public class Person {
private String userid;
private String username;
private String password;
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "Person{" +
"userid='" + userid + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
public Person(String userid, String username, String password) {
this.userid = userid;
this.username = username;
this.password = password;
}
public Person() {
}
}
package com.chen.dao;
import com.chen.pojo.Person;
import java.util.List;
public interface PersonDao {
List<Person> getPersonList();
}
<?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接口-->
<mapper namespace="com.chen.dao.PersonDao">
<select id="getPersonList" resultType="com.chen.pojo.Person">
select * from Person
</select>
</mapper>
注意点:org.apache.ibatis.binding.BindingException: Type interface com.chen.dao.PersonDao is not known to the MapperRegistry.
package chen.dao;
import chen.pojo.Person;
import chen.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class PersonTest {
@Test
public void test(){
//第一步,获取sqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//第二步,执行sql
PersonDao personDao = sqlSession.getMapper(PersonDao.class);
List<Person> personList = personDao.getPersonList();
for (Person person : personList) {
System.out.println(person);
}
//最后,关闭sqlSession
sqlSession.close();
}
}
public interface PersonMapper {
// 增
public void addPerson(Map<String,Object> map);
// 删
public void delPerson(String id);
// 改
public void updatePerson(Map<String,Object> map);
// 查
List<Person> getPersonList();
}
<mapper namespace="chen.dao.PersonMapper">
<insert id="addPerson" parameterType="map">
insert into Person(userid,username,password) values(#{
id},#{
name},#{
password})
</insert>
<delete id="delPerson" parameterType="String">
delete from Person where userid = #{
userid}
</delete>
<update id="updatePerson" parameterType="map">
update Person set username = #{
name} where userid = #{
id}
</update>
<select id="getPersonList" resultType="chen.pojo.Person">
select * from Person
</select>
</mapper>
public class PersonTest {
@Test
public void testGetPerson(){
//第一步,获取sqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//第二步,执行sql
PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
List<Person> personList = personMapper.getPersonList();
for (Person person : personList) {
System.out.println(person);
}
//最后,关闭sqlSession
sqlSession.close();
}
@Test
public void testAddPerson(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
Map<String,Object> map = new HashMap<String, Object>();
map.put("id","104");
map.put("name","陈志辉");
map.put("password","123456789");
personMapper.addPerson(map);
sqlSession.commit();
sqlSession.close();
}
@Test
public void testDelPerson(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
personMapper.delPerson("104");
sqlSession.commit();
sqlSession.close();
}
@Test
public void testUpdatePerson(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
Map<String,Object> map = new HashMap<String, Object>();
map.put("id","2017110110");
map.put("name","陈志辉");
personMapper.updatePerson(map);
sqlSession.commit();
sqlSession.close();
}
}
// 模糊查询
List<Person> getPersonList2(String value);
<select id="getPersonList2" resultType="chen.pojo.Person">
select * from Person where username like "%"#{
value}"%"
</select>
@Test
public void testGetPerson2(){
//第一步,获取sqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//第二步,执行sql
PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
List<Person> personList = personMapper.getPersonList2("辉");
for (Person person : personList) {
System.out.println(person);
}
//最后,关闭sqlSession
sqlSession.close();
}