1、mybatis由来和介绍优缺点
mybatis使用一个数据持久化的框架整合了jdbc。让使用者关注与sql语句的处理而不需要携大量的jdbc相关的语句。
优点:半自动化的ORM对象。对象实体与数据库对象相对应。简单易实现。
主要是xml和注解的方式实现mybatis数据的驱动
细节注意
1xml配置时如果在java如下
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
或者在resource目录下创建
这样上面就不需要在pop.xml添加因为扫描器不会扫描java包下的xml文件。
但是包要一级一级创建如下创建错误
因为这只是创建一级目录只不过是用 **.**隔开就会报出找不到com.example.springboot.mappers包
2、mybatis依赖
<!-- 添加mybatis坐标-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<!-- 数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
使用xml
3、mybatis-config配置
<?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>
<properties resource="application.properties"></properties>
<typeAliases>
<package name="com.example.springboot.pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 使用xml文件实现mapper文件配置如下 resource对应的值是包名和对相应xml文件名-->
<mapper resource="com/example/springboot/mappers/AyUserMapper.xml"></mapper>
<!-- 使用注解实现mapper配置如下 class对应的值是注解所在包名和对象名-->
<mapper class="com.example.springboot.mappers.AyUserMapper"/>
</mappers>
</configuration>
4、*mapper.xml相关文件(如果该文件放在java目录下需要在pop.xml文件加入相应配置请到下面6中找)
<?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.example.springboot.mappers.AyUserMapper">
<select id="findNameById" parameterType="int" resultType="Info">
select * from info where id=#{id}
</select>
<select id="findAll" resultType="Info">
select * from info
</select>
<insert id="addInfo" parameterType="Info" >
insert into info(id,name) values (#{id},#{name})
</insert>
</mapper>
5、相关增删改查案例
package com.example.springboot;
import com.example.springboot.mappers.AyUserMapper;
import com.example.springboot.pojo.Info;
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.After;
import org.junit.Before;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;
public class MybatisTest {
private SqlSessionFactory sqlSessionFactory;
private SqlSession sqlSession;
@Before
public void init() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
sqlSession=sqlSessionFactory.openSession();
}
@Test
public void testFindNameById() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
sqlSession=sqlSessionFactory.openSession();
System.out.println(sqlSessionFactory);
System.out.println(sqlSession);
Info info=sqlSession.selectOne("findNameById",101);
System.out.println(info);
}
@Test
public void testFindAll() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
sqlSession=sqlSessionFactory.openSession();
List<Info> infos = sqlSession.selectList("findAll");
infos.forEach(info -> {
System.out.println(info);
});
}
@Test
public void testInsert() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
sqlSession=sqlSessionFactory.openSession();
Info info=new Info(8,"李四");
int i = sqlSession.insert("addInfo", info);
System.out.println("修改成功"+i);
}
/**
使用代理对象实现数据
*/
@Test
public void testFindnameByIDTwo() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
sqlSession=sqlSessionFactory.openSession();
AyUserMapper mapper = sqlSession.getMapper(AyUserMapper.class);
Info info = mapper.findNameById(101);
System.out.println(info);
}
@After
public void destory(){
sqlSession.commit();
sqlSession.close();
}
}
6、使用注解比较简单
在mybatis_config.xml文件加入
在mapper class对应包下使用注解如下
@Mapper
public interface AyUserMapper {
@Select("select * from info where id=#{id}")
Info findNameById(int id);
}
测试类中测试方法
public void testAnnoation() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession=sqlSessionFactory.openSession();
AyUserMapper mapper = sqlSession.getMapper(AyUserMapper.class);
Info info = mapper.findNameById(101);
System.out.println(info);
}