0
点赞
收藏
分享

微信扫一扫

总结MyBatis知识点

zmhc 2022-04-06 阅读 38

目录

1、 MyBatis是什么?

2、Mybatis解决哪些问题?

3、开始创建工程

【1】创建Maven项目

【2】pom.xml依赖

 【3】mybatis.xml依赖

【4】实体类

 【6】测试类

【7】发现一个BUG,Team.xml

【8】结果

 【9】改进,使用@Before@After

 4、日志的使用

【1】pom.xml

【2】log4j.properties

【3】mybatis.xml

【4】结果

 5、建造者设计模式介绍

6、#{} 和 ${}的区别

【1】#{}

【2】${}

7、动态SQL

更新

8、分页

【1】pom.xml

【2】mybatis.xml

【3】测试

9、Mybatis缓存

【1】 一级缓存

【2】二级缓存

【3】清空缓存的方式


1、 MyBatis是什么?

官网:mybatis – MyBatis 3 | 简介

2、Mybatis解决哪些问题

3、开始创建工程

【1】创建Maven项目

【2】pom.xml依赖

<?xml version="1.0" encoding="UTF-8"?>
<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>com.qinluyu</groupId>
    <artifactId>Mybatis01</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--Mybatis-->
    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.7</version>
        </dependency>
        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <!--mysql版本5-6之间-->
            <version>5.1.45</version>
        </dependency>
        <!--测试test-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <!--maven-->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <!--jdk-->
                    <target>1.8</target>
                    <source>1.8</source>
                </configuration>

            </plugin>
        </plugins>
    </build>
</project>

 【3】mybatis.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.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false&amp;serverTimezone=GMT"/>
                <property name="username" value="${root}"/>
                <property name="password" value="${123}"/>
            </dataSource>
        </environment>
    </environments>
    <!--映射文件-->
    <mappers>
        <mapper resource="com.qinluyu.Dao.Team.xml"/>
    </mappers>
</configuration>

【4】实体类

public class Team {
    private Integer teamId;
    private String teamName;
    private String location;
    private Date createTime;
get/set方法
tostring方法

【5】实体类的映射文件

 【6】测试类

private String resource="mybatis.xml";
    @Test
    public void test02(){
        SqlSession sqlSession=null;
        try{
        Reader reader=Resources.getResourceAsReader(resource);
        SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(reader);//根据图纸创建出了工厂
        sqlSession=factory.openSession();
        //执行sql
        List<Team> list=sqlSession.selectList("com.qinluyu.Dao.Team.queryAll");
        //遍历结果
        for(Team team:list){
        System.out.println(team);
        }
        }catch(IOException e){
        e.printStackTrace();
        }finally{
        //关闭资源
        sqlSession.close();
        }

【7】发现一个BUG,Team.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.qinluyu.pojo.Team">
    <!--查询,id为自定义名称,相当于dao-->
    <select id="queryAll" resultType="com.qinluyu.pojo.Team">

    <!--<select id="queryAll" resultMap="com.qinluyu.pojo.Team">-->此为BUG
        select * from team;
    </select>

</mapper>

【8】结果

 【9】改进,使用@Before@After

public class TestTeam {
    private String resource="mybatis.xml";
    private SqlSession sqlSession;
    @Test
    public void test02(){
        System.out.println("改进————,查询单个");
        Team team = sqlSession.selectOne("com.qinluyu.pojo.Team.queryOne", 1002);
        System.out.println(team);
    }
    @Before
    public void before(){
        Reader reader=null;
        try {
            reader=Resources.getResourceAsReader(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(reader);
        sqlSession=factory.openSession();
    }
    @After
    public void after(){
        sqlSession.close();
    }

 4、日志的使用

【1】pom.xml

<!--日志的使用-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

【2】log4j.properties

# Global logging configuration  info  warning  error
log4j.rootLogger=DEBUG,stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

【3】mybatis.xml

<!--注意顺序
    configuration (properties?, settings?, typeAliases?, typeHandlers?, objectFactory?, objectWrapperFactory?, reflectorFactory?, plugins?, environments?, databaseIdProvider?, mappers
    -->
    <settings>
        <setting name="logImpl" value="LOG4J"/>
    </settings>

【4】结果

 5、建造者设计模式介绍

6、#{} ${}的区别

【1】#{}

【2】${}

7、动态SQL

更新

<update id="updateAuthorIfNecessary">
  update Author
    <set>
      <if test="username != null">username=#{username},</if>
      <if test="password != null">password=#{password},</if>
      <if test="email != null">email=#{email},</if>
      <if test="bio != null">bio=#{bio}</if>
    </set>
  where id=#{id}
</update>

8、分页

【1】pom.xml

<dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.2.0</version>
        </dependency>

【2】mybatis.xml

<!--分页-->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
        <!--<property name="reasonable" value="true"/>-->
    </plugins>

【3】测试

public void test5() {
        // PageHelper.startPage 必须紧邻查询语句,而且只对第一条查询语句生效
        PageHelper.startPage(2, 5);
        List<Team> teams = teamMapper.queryAll();//查询语句结尾不能有分号
        teams.forEach(team -> System.out.println(team));
        PageInfo<Team> info = new PageInfo<>(teams);
        System.out.println("分页信息如下:");
        System.out.println("当前页:" + info.getPageNum());
        System.out.println("总页数:" + info.getPages());
        System.out.println("前一页:" + info.getPrePage());
        System.out.println("后一页:" + info.getNextPage());
        System.out.println("navigatepageNums:" + info.getNavigatepageNums());
        for (int num : info.getNavigatepageNums()) {
            System.out.println(num);
        }

9、Mybatis缓存

【1】 一级缓存

【2】二级缓存

【3】清空缓存的方式

后续还有补充,有点难,先停一下

举报

相关推荐

0 条评论