欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mybatis 注解方式,增,删,改,全查,单查,模糊,总数,CRUD操作

程序员文章站 2022-05-08 17:30:01
...

1.搭建maven ,导入相关jar依赖

<!--打包成jar包-->
    <packaging>jar</packaging>
    <!--所有相关jar包依赖和下载的区域-->
    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <dependency>
            <!--日志包-->
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>
        <dependency>
            <!--单元测试包-->
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>
    </dependencies>

mybatis 注解方式,增,删,改,全查,单查,模糊,总数,CRUD操作2.导入log4j的固定日志文件,具体参照,https://mp.csdn.net/mdeditor/95371441# 文档中有这个知识点。
配置连接数据库信息的mybatis 主配置文件 名:sqlMapConfig.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">
<!-- mybatis的主配置文件 -->
<configuration>
    <!--配置实体类路径的别名-->
    <typeAliases>
        <package name="entity"></package>
    </typeAliases>
    <!-- 配置环境 -->
    <environments default="mysql">
        <!-- 配置mysql的环境-->
        <environment id="mysql">
            <!-- 配置事务的类型-->
            <transactionManager type="JDBC"></transactionManager>
            <!-- 配置数据源(连接池) -->
            <dataSource type="POOLED">
                <!-- 配置连接数据库的4个基本信息 -->
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/test"/>
                <property name="username" value="root"/>
                <property name="password" value="123"/>
            </dataSource>
        </environment>
    </environments>

    <!-- 指定映射配置中所有接口的位置 -->
    <mappers>
        <package name="dao"></package>
    </mappers>
</configuration>

mybatis 注解方式,增,删,改,全查,单查,模糊,总数,CRUD操作3.创建实体类包和类。并实现序列化接口。
mybatis 注解方式,增,删,改,全查,单查,模糊,总数,CRUD操作4.写接口类里面的方法,和注解,和sql 指令

public interface IStudentDao {
   //查询所有学生
    @Select("select * from student")
    List<Student> query();
    // 新增学生
    @Insert( "insert into student (id,name,age,sex) " +
            " values(#{id},#{name},#{age},#{sex});")
    void add(Student student);
    //修改
    @Update("update student set name = #{name},age = #{age},sex = #{sex} where id = #{id};")
    void update(Student student);
    //删除
    @Delete("delete from student where id = #{id};")
    void delete(int id);
    //单查学生
    @Select("select * from student  where id = #{id}")
    Student findbyid(int id);
    //名字模糊查询
            //1.模糊条件需要,在前台,组装为 《'%经%'》 格式
        @Select("select * from student  where name like(#{name});")
            //2.模糊条件前台传入值为value,后台语句,定义好的格式,直接引用值
    //  @Select("select * from student  where name like '%${value}%'  ")
    List<Student> *(String name);
    //查询学生数量
    @Select("select count(*) from student")
    int count();
}

mybatis 注解方式,增,删,改,全查,单查,模糊,总数,CRUD操作
5.生成相应的测试方法。mybatis 注解方式,增,删,改,全查,单查,模糊,总数,CRUD操作
day02,补充
1.表间关系,一对一的操作。
mybatis 注解方式,增,删,改,全查,单查,模糊,总数,CRUD操作

@Select("select * from Account")
    @Results(id = "accountMap" ,  value = {
        @Result(id = true,property = "id" , column = "id"),
        @Result(property = "money" , column = "money"),
        @Result(property = "stuId" , column = "stuId"),
        @Result(property = "student" ,column = "stuId",
                one = @One(select = "dao.IStudentDao.findbyid",
                        fetchType = FetchType.EAGER))
    })
    List<Account> query();

2.表间关系,一对多的操作。
mybatis 注解方式,增,删,改,全查,单查,模糊,总数,CRUD操作

//查询所有学生
    @Select("select * from student")
    @Results(id = "studentMap", value = {
        @Result(id = true , property = "id" ,column = "id"),
        @Result( property = "name" ,column = "name"),
        @Result( property = "age" ,column = "age"),
        @Result( property = "sex" ,column = "sex"),
        @Result( property = "accounts" ,column = "id",
                many = @Many(select = "dao.IAccountDao.findbyid",
                        fetchType = FetchType.LAZY))
    })
    List<Student> query();

3.配置二级缓存

先去主配置文件中,把cacheEnabled ,的值设为true ,开启二级缓存

mybatis 注解方式,增,删,改,全查,单查,模糊,总数,CRUD操作

再把接口类的二级缓存,设置为打开,默认是关闭的
    @CacheNamespace(blocking = true)

mybatis 注解方式,增,删,改,全查,单查,模糊,总数,CRUD操作