当前位置: 首页 > article >正文

木舟0基础学习Java的第二十八天(常见的Java框架,MyBatis框架,动态SQL,缓存机制,多表关联查询,注释开发,逆向工程,LOG4J,Lombok)

常见的Java开发的框架

MyBatis,ORM(Object Relational Mapping)框架,底层对jdbc进行了封装

Spring 是一个容器(可以将其他框架融合在一起)

SpringMVC 分层框架

Struts2 功能类似于 SpringMVC

Hibernate 功能类似于 MyBatis

MyBatis

持久层(数据访问层)框架 支持自定义sql 存储过程以及高级映射

通过xml或者注解配置原始类型 接口 Java pojo为数据库中的记录

MyBatis中三个常用的查询方法

1.selectOne

用于查询单条数据的情况,返回值是一个对象,如果没有查到任何数据,返回 null

2.selectList

用于查询多条数据的情况,返回值是一个 list 集合,如果没有查到任何数据返回没有元素的集合(空集合,不是 null)

3.selectMap

用于查询多条数据的情况,多条数据要形成一个 Map集合需要指定哪个属性作为 key 如果查不到,返回一个空 map 集合(不是 null)

MyBatis中常用的DML方法

1.insert
2.delete
3.update

DML要考虑事务处理(从mybatis的源码里看到事务默认是手动提交).commit

增删改查例:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis.mapper.UserMapper"><!--写该xml的路径-->
    <select id="selAll" resultType="com.mybatis.pojo.Users"><!--自动封装为集合 resultType为泛型-->
        select * from t_user<!--不需要分号结尾 会报错-->
    </select>
    <select id="selOne" resultType="com.mybatis.pojo.Users"><!--自动封装为集合 resultType为泛型-->
        select * from t_user where id=13<!--不需要分号结尾 会报错-->
    </select>
    <insert id="addUser">
        insert into t_user(id,uname,pwd) values(null,"妮妮","0105")
    </insert>
    <update id="updateUser">
        update t_user set uname="nini" where uname="妮妮"
    </update>
    <delete id="deleteUser">
        delete from t_user where id=13
    </delete>
</mapper>
public class Demo01 {
    public static void main(String[] args) {
        //加载mybatis.xml的配置文件
        String resource = "mybatis.xml";//mybatis.xml要放在src目录下
        try {
            //把配置文件转换为流 相当于FileInputStream
            InputStream is = Resources.getResourceAsStream(resource);
            //通过xml配置文件的数据创建SqlSessionFactory对象
            SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(is);
            //由SqlSessionFactory对象创建发送sql语句的对象
            SqlSession sqlSession = sqlSessionFactory.openSession();
            //查询多条数据 返回list集合
            List<Users> users= sqlSession.selectList("selAll");
            //selAll由UserMapper.xml中的select id="selAll"取到
            //查询单条数据
            Object selOne = sqlSession.selectOne("selOne");
            /*
            查询多条数据 返回map集合
            第一个参数:映射文件UserMapper.xml中的sql语句对应的id
            第二个参数:指定那么列(属性) 作为map中的key
            */
            Map<Integer, Users> map = sqlSession.selectMap("selAll", "id");
            System.out.println(users);
            System.out.println(selOne);
            System.out.println(map);
            sqlSession.commit(true);//设置mybatis事物为自动提交(一般推荐手动提交)
            sqlSession.close();//关闭资源
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

public class Demo02 {
    SqlSession sqlSession;
    public Demo02() {
        String resource="mybatis.xml";
        try {
            InputStream is = Resources.getResourceAsStream(resource);

            SqlSessionFactory ssf=new SqlSessionFactoryBuilder().build(is);
            sqlSession = ssf.openSession();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //新增
    @Test
    public void addUser(){
        int addUser = sqlSession.insert("addUser");
            System.out.println(addUser==1?"新增成功":"新增失败");
        //增删改是 有事物的 需要commit 手动提交事物
        sqlSession.commit();
        sqlSession.close();
    }
    //修改
    @Test
    public void updateUser(){
        int updateUser= sqlSession.update("updateUser");
        System.out.println(updateUser==1?"修改成功":"修改失败");
        //增删改是 有事物的 需要commit 手动提交事物
        sqlSession.commit();
        sqlSession.close();
    }
    //删除
    @Test
    public void deleteUser(){
        int deleteUser= sqlSession.delete("deleteUser");
        System.out.println(deleteUser==1?"删除成功":"删除失败");
        //增删改是 有事物的 需要commit 手动提交事物
        sqlSession.commit();
        sqlSession.close();
    }
}

其他标签(了解)

properties:用于加载外部的properties文件 通过${}方式获取

typeAliases:配置简化返回值类型

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--从当前文件以外加载数据库参数的配置文件-->
    <properties resource="db.properties"></properties>
    <!--给全路径类名取别名:尽量使用类名-->
    <typeAliases>
        <typeAlias type="com.mybatis.pojo.T_user" alias="T_user"></typeAlias>
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <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="com/mybatis/mapper/UserMapper.xml"/>
    </mappers>
</configuration>
 <select id="selAll" resultType="T_user">
        select * from t_user
    </select>

 通过接口绑定映射文件 进行crud(增删改查)操作

开发中推荐使用 比session提供的方法更方便

xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis.mapper.UserMapper"><!--写该xml的路径-->
   <!-- 查询-->
    <select id="selAll" resultType="Users"><!--自动封装为集合 resultType为泛型-->
        select * from t_user<!--不需要分号结尾 会报错-->
    </select>
    <select id="login" resultType="Users">
        select * from t_user where uname=#{param1} and pwd=#{param2}
    </select>
  <!--  如果传入的参数是对象类型 动态取值(推荐使用#) 使用对象中对应的属性名称-->
    <select id="login2" resultType="Users">
        select * from t_user where uname=#{uname} and pwd=#{pwd}
    </select>
    <!--新增-->
    <insert id="addUser">
        insert into t_user(id,uname,pwd) values(#{param1},#{param2},#{param3})
    </insert>
    <insert id="addUser2">
        insert into t_user values(#{id},#{uname},#{pwd})
    </insert>
    <!--修改-->
    <update id="updateUser">
        update t_user set pwd=#{param2} where id=#{param1}
    </update>
    <update id="updateUser2">
        update t_user set pwd=#{pwd},uname=#{uname} where id=#{id}
    </update>
    <!--删除-->
    <delete id="deleteUser">
        delete from t_user where uname=#{param1}
    </delete>
    <delete id="deleteUser2">
        delete from t_user where uname=#{uname}
    </delete>
</mapper>

 接口

//当前接口与UserMapper.xml文件绑定起来
public interface UserMapper {
    //查询
    public List<Users> selAll();//抽象方法名需要与配置文件中的id一致
    public Users login(String uname, String pwd);
    public Users login2(Users u);
    //添加
    public int addUser(int id, String uname, String pwd);
    public int addUser2(Users u);
    //修改
    public int updateUser(int id,String pwd);
    public int updateUser2(Users u);
    //删除
    public int deleteUser(String uname);
    public int deleteUser2(Users u);
}

测试

public class Demo03 {
    SqlSession sqlSession;
    UserMapper mapper;
    public Demo03() {
        String resource="mybatis.xml";
        try {
            InputStream is = Resources.getResourceAsStream(resource);
            SqlSessionFactory ssf=new SqlSessionFactoryBuilder().build(is);
            sqlSession = ssf.openSession();
            mapper = sqlSession.getMapper(UserMapper.class);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    @Test
    public void selAllUser(){
        List<Users> users = mapper.selAll();
        System.out.println(users);
    }
    @Test
    public void loginUser(){
        Users admin = mapper.login("admin", "1234");
        System.out.println(admin);
    }
    @Test
    public void loginUser2(){
        Users u=new Users();
        u.setUname("张三");
        u.setPwd("3333333");
        Users users = mapper.login2(u);
        System.out.println(users);
    }


    @Test
    public void addUser(){
        int i = mapper.addUser(18, "木舟", "123");
        System.out.println(i==1?"添加成功":"添加失败");
        sqlSession.commit();
    }
    @Test
    public void addUser2(){
        Users u=new Users();
        u.setId(18);
        u.setUname("木舟");
        u.setPwd("123");
        int i = mapper.addUser2(u);
        System.out.println(i==1?"添加成功":"添加失败");
        sqlSession.commit();
    }


    @Test
    public void updateUser(){
        int i = mapper.updateUser(1, "333");
        System.out.println(i==1?"修改成功":"修改失败");
        sqlSession.commit();

    }
    @Test
    public void updateUser2(){
        Users u=new Users();
        u.setPwd("55555");
        u.setUname("月木舟");
        u.setId(18);
        int i = mapper.updateUser2(u);
        System.out.println(i==1?"修改成功":"修改失败");
        sqlSession.commit();
    }


    @Test
    public void deleteUser(){
        int i = mapper.deleteUser("木舟");
        System.out.println(i==1?"删除成功":"删除失败");
        sqlSession.commit();
    }
    @Test
    public void deleteUser2(){
        Users u=new Users();
        u.setUname("月木舟");
        int i = mapper.deleteUser2(u);
        System.out.println(i==1?"删除成功":"删除失败");
        sqlSession.commit();
    }
}

动态SQL

<if>标签

​
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis.mapper.UserMapper"><!--写该xml的路径-->
   <!-- 查询-->
    <select id="selByPram" resultType="Users"><!--自动封装为集合 resultType为泛型-->
        select * from t_user where 1=1
        <if test="id!=0">
           and id=#{id}
        </if>
        <if test="uname!=null and uname!=''">
            and uname=#{uname}
        </if>
        <if test="pwd!=null and pwd!=''">
            and pwd=#{pwd}
        </if>
    </select>
</mapper>

​

<where>标签 

如果没有条件,不会生成 where关键字

如果有条件,会自动添加 where 关键字

如果条件中有 and,自动去除之,但不会自动加

  <select id="selByPram2" resultType="Users"><!--自动封装为集合 resultType为泛型-->
        select * from t_user
        <where><!--可以自动去掉and 根据条件判断是否保留and-->
        <if test="id!=0">
            and id=#{id}
        </if>
        <if test="uname!=null and uname!=''">
            and uname=#{uname}
        </if>
        <if test="pwd!=null and pwd!=''">
            and pwd=#{pwd}
        </if>
        </where>
    </select>

<bind>标签

用于模糊查询

可以对传入的参数提前处理

方式1
<!--方式1-->
    <select id="selLike" resultType="Users">
        <bind name="param1" value="'%'+param1+'%'"/><!--通过bind绑定标签 进行数据提前加工-->
        select * from t_user where uname like #{param1}
    </select>
方式2

推荐用这个可读性强

<!--方式2:使用concat函数拼接-->
    <select id="selLike2" resultType="Users">
        select * from t_user where uname like concat('%',#{param1},'%')
    </select>

<foreach>标签

在一个范围中查询
用于在 SQL 语句中遍历集合参数,在 in 查询中使用
1.collection:待遍历的集合

2.open:设置开始符号(

3.separator:项目分隔符,

4.close:设置结束符号)

5.item:迭代变量

<select id="selIn" resultType="Users">
        select * from t_user where id in
        <foreach collection="list" open="(" separator="," close=")" item="u"><!--u用来接受list集合中的元素-->
            #{u}
        </foreach>
    </select>

<sql><include>标签

动态选择要查询的列

 <sql id="id1">
        id,uname
    </sql>
    <sql id="id2">
        uname,pwd
    </sql>
    <select id="selSame" resultType="Users">
        select
        <include refid="id2">
            <!--代替*-->
            <!--refid 关联 sql的id 查询想查的列-->
        </include>
        from t_user
    </select>

<choose><when><otherwise>标签(了解) 

但条件查询 只取其一   

类似于if--else--if--else

 <select id="selByPram3" resultType="Users"><!--自动封装为集合 resultType为泛型-->
        select * from t_user
        <where><!--可以自动去掉and 根据条件判断是否保留and-->
            <choose>
            <when test="id!=0">
                and id=#{id}
            </when>
            <when test="uname!=null and uname!=''">
                and uname=#{uname}
            </when>
            <otherwise >
                and pwd=#{pwd}
            </otherwise>
            </choose>
        </where>
    </select>

只要满足条件 就不再向下运行 不追加其他条件 如果都不满足就不查询数据 

<set>标签

用于维护 update 语句中的 set 子句

1.满足条件时,会自动添加 set 关键字

2.会去除 set 子句中多余的逗号

3.不满足条件时,不会生成 set 关键字

<update id="updateUser">
        update t_user
        <set>
            <if test="uname!=null and uname!=''">
                uname=#{uname},
            </if>
            <if test="pwd!=null and pwd!=''">
                pwd=#{pwd}
            </if>
        </set>
       where id=#{id}
    </update>

查询过程中 类中属性名与数据库中列名不一致 解决方式

建议开发中类中的属性名与sql中的列名一致

<resultMap id="mm" type="com.mybatis.pojo.T_User">
        <id property="uid" column="id"></id><!--主键列 实体类uid关联数据库id-->
        <result property="username" column="uname"></result><!--非主键列 实体类username关联数据库uname-->
        <result property="password" column="pwd"></result><!--非主键列 实体类password关联数据库pwd-->
    </resultMap>
    <select id="selAll" resultMap="mm"><!--mm关联resultMap中id名称-->
        select * from t_user
    </select>

 MyBatis的缓存机制

MyBatis的缓存是使用SQL标签的ID作为缓存的唯一标识,执行相同的标签可以使用缓存,不同的标签不能使用缓存

注意:使用效果有限 MyBatis缓存不能持久化保持

MyBatis中有两种缓存机制:

1.一级缓存

默认开启 线程级别的缓存 SqlSession的缓存

在SqlSession的生命周期中有效 SqlSession关闭 缓存清空

2.二级缓存

进程级别的缓存 SqlSessionFactory的缓存(缓存数据存储在这个对象中)

在一个SqlSessionFactory生命周期中有效 可以在多个SqlSession生命周期中共享

默认关闭 需要使用的时候 要为某个命名空间 开启二级缓存(在mapper.xml中配置<cache>)

<mapper namespace="com.mybatis.mapper.UserMapper">
    <!--开启二级缓存-->
<cache></cache>

MyBatis多表关联查询

多对一

1.业务装配实现多表查询(多对一)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis.mapper.StuMapper">
    <select id="getStudents" resultType="stu">
        select * from student
    </select>
</mapper>
@Data
public class Student implements Serializable {
    private int sid;
    private String sname;
    private int age;
    private int cid;

    //持有班级信息作为属性
    private Clazz clazz;
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis.mapper.ClazMapper">
    <select id="selBy" resultType="cla">
        select * from clazz where cid = #{param1}
    </select>
</mapper>
  /*
    手动装配方式的多对一查询
    查询学生对应的班级信息
    */
    @Test
    public void sel1(){
        //1.查询所有的学生信息
        List<Student> students = stuMapper.getStudents();
        for (Student stu : students) {
            //每个学生的cid
            int cid = stu.getCid();
        //2.根据cid查对应的班级
            Clazz clazz = clazMapper.selBy(cid);
            stu.setClazz(clazz);
        }
        System.out.println(students);
    }

 2.resultMap的N+1方式实现多表查询(多对一)

  <!--N+1的装配方式-->
    <resultMap id="rm" type="stu">
        <!--如果Student中的属性名与数据库中的类名一致可以省略映射关系-->
       <!-- <id property="sid" column="sid"></id>
        <result property="sname" column="sname"></result>
        <result property="age" column="age"></result>
        <result property="cid" column="cid"></result>-->

        <!--以下的意思是给Student类中的属性clazz赋值
        值通过com.mybatis.mapper.ClazMapper中的 标签id为selBy查询
        查询出的数据封装类型为cla
        根据列名cid查-->
        <association property="clazz" select="com.mybatis.mapper.ClazMapper.selBy" javaType="cla" column="cid">
            <!--如果Clazz中的属性名与数据库中的类名一致可以省略映射关系-->
            <!--<id property="cid" column="cid"></id>
            <result property="cname" column="cname"></result>
            <result property="room" column="room"></result>-->
        </association>
    </resultMap>
    <select id="getStudents2" resultMap="rm">
        select * from student
    </select>

 3.使用sql多表关联语句查询(多对一)

  <!--使用sql多表关联语句查询-->
    <resultMap id="rm2" type="stu">
        <!--这里的映射文件不能省略-->
        <id property="sid" column="sid"></id>
        <result property="sname" column="sname"></result>
        <result property="age" column="age"></result>
        <result property="cid" column="cid"></result>
        <association property="clazz" javaType="cla">
            <!--这里的映射文件不能省略-->
            <id property="cid" column="cid"></id>
            <result property="cname" column="cname"></result>
            <result property="room" column="room"></result>
        </association>
    </resultMap>
    <select id="getStudents3" resultMap="rm2">
        select * from student natural join clazz
    </select>

一对多 

1.业务装配实现多表查询(一对多)

 <!--手动装配实现一对多-->
    <select id="selClazzAll" resultType="cla">
        select * from clazz
    </select>
@Data
public class Clazz {
    private int cid;
    private String cname;
    private String room;

    //持有当前班级中的学生信息(一对多)
    private List<Student> stus;
}
<!--                                 一对多                             -->
    <!--根据班级cid 查学生-->
    <select id="getStus" resultType="stu">
        select * from student where cid=#{param1}
    </select>

  /*
    手动装配方式的一对多查询
    查询班级及其班级中包含的学生
    */
    @Test
    public void sel4(){
        //1.查所有的班级信息
        List<Clazz> clazzes = clazMapper.selClazzAll();
        for(Clazz clazz:clazzes){
            //得到cid
            int cid = clazz.getCid();
            //根据cid查学生
            List<Student> stus = stuMapper.getStus(cid);
            clazz.setStus(stus);
        }
        System.out.println(clazzes);
    }

 2.resultMap的N+1方式实现多表查询(一对多)

 <!--N+1的装配方式-->
    <resultMap id="aa" type="cla">
        <!--在一对多中 id不能省略-->
        <!--类属性名与数据库列名 如果一致 除了主键其他映射关系可以省略-->
        <id property="cid" column="cid"></id>
        <collection property="stus" select="com.mybatis.mapper.StuMapper.getStus"  column="cid">
            <id property="sid" column="sid"></id>
        </collection>
    </resultMap>
    <select id="selClazzAll2" resultMap="aa">
        select * from clazz
    </select>

 3.使用sql多表关联语句查询(一对多)

<!--一对多的sql多表关联语句查询 注意:查询的结果中不能有同名列-->
    <resultMap id="bb" type="cla">
        <id property="cid" column="cid"></id>
        <result property="cname" column="cname"></result>
        <result property="room" column="room"></result>
        <collection property="stus" javaType="list" ofType="stu">
            <id property="sid" column="sid"></id>
            <result property="sname" column="sname"></result>
            <result property="age" column="age"></result>
            <result property="cid" column="cid"></result>
        </collection>
    </resultMap>
    <select id="selClazzAll3" resultMap="bb">
        select c.cid,c.cname,c.room,s.sid,s.sname,s.age from student s join clazz c on c.cid=s.cid
    </select>

注解开发

用注解完成增删改查

优点:简洁,可以不使用****Mapper.xml映射文件

缺点:多表查询特别繁琐

建议在单表查询时使用

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <properties resource="db.properties"></properties>
    <typeAliases>
        <typeAlias type="com.mybatis.pojo.Users" alias="u"></typeAlias>
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <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>
        <!--扫描当前com.mybatis.mapper包下所有的mapper.xml文件-->
        <package name="com.mybatis.mapper"/>
    </mappers>
</configuration>
public interface UserMapper {
    //查所有
    @Select("select * from t_user")
    public List<Users> getUsers();
    //根据名字查询
    @Select("select * from t_user where uname=#{param1}")
    public List<Users> getUsersByName(String uname);
    //新增
    @Insert("insert into t_user values(null,#{uname},#{pwd})")
    public int addUsers(Users users);
    //修改
    @Update("update t_user set pwd=#{param1} where uname=#{param2}")
    public int updateUsers(String pwd, String uname);
    //删除
    @Delete("delete from t_user where uname=#{param1}")
    public int deleteUsers(String uname);
public class Demo {
    UserMapper mapper;
    SqlSession sqlSession;
    public Demo() {
        String resource ="mybatis.xml";
        try {
            InputStream is = Resources.getResourceAsStream(resource);
            SqlSessionFactory ssf=new SqlSessionFactoryBuilder().build(is);
            sqlSession = ssf.openSession();
            mapper=sqlSession.getMapper(UserMapper.class);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //查所有
    @Test
    public void selAll(){
        List<Users> list = mapper.getUsers();
        System.out.println(list);
    }
    //根据名字查询
    @Test
    public void selUname(){
        List<Users> list = mapper.getUsersByName("张三");
        System.out.println(list);
    }
    //新增
    @Test
    public void insert(){
        Users user = new Users();
        user.setUname("李雷");
        user.setPwd("123456");
        int i = mapper.addUsers(user);
        sqlSession.commit();
        System.out.println(i==1?"新增成功":"新增失败");
    }
    //修改
    @Test
    public void update(){
        int i = mapper.updateUsers("123", "李雷");
        sqlSession.commit();
        System.out.println(i==1?"修改成功":"修改失败");
    }
    //删除
    @Test
    public void delete(){
        int i = mapper.deleteUsers("李雷");
        sqlSession.commit();
        System.out.println(i==1?"删除成功":"删除失败");
    }
}

MyBatis逆向工程

注意:逆向工程只适合单表查询,不适合多表关联查询(多表使用业务装配方式完成)

public class Demo {
    SqlSession sqlSession;
    ClazzMapper clazzMapper;
    StudentMapper studentMapper;
    public Demo() {
        String resource = "mybatis.xml";
        try {
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory ssf=new SqlSessionFactoryBuilder().build(inputStream);
            sqlSession = ssf.openSession();
            clazzMapper = sqlSession.getMapper(ClazzMapper.class);
            studentMapper = sqlSession.getMapper(StudentMapper.class);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //查询所有的学生信息
    @Test
    public void selAll(){
        //selectByExample根据非主键列查询 查所有 传null
        List<Student> students = studentMapper.selectByExample(null);
        System.out.println(students);
    }
    //根据学生的sid和sname查询
    @Test
    public void selIdName(){
        //用一个对象StudentExample 封装查询参数
        StudentExample exp = new StudentExample();
        //                   传入要查询的sid         传入要查询的sname
        exp.createCriteria().andSidEqualTo(7).andSnameEqualTo("妮妮");
        //exp.createCriteria().andSnameEqualTo("妮妮");
        List<Student> students = studentMapper.selectByExample(exp);
        if (students!=null&&students.size()>0){
            Student student = students.get(0);
            System.out.println(students);
        }
    }
    //根据主键sid查询
    @Test
    public void selId(){
        Student student = studentMapper.selectByPrimaryKey(1);
        System.out.println(student);
    }
    //查询
    @Test
    public void sel1(){
        //年龄>=25的学生
        StudentExample exp1 = new StudentExample();
        exp1.createCriteria().andAgeGreaterThanOrEqualTo(25);
        //查询年龄>=23 <=25
        StudentExample exp2 = new StudentExample();
        exp2.createCriteria().andAgeGreaterThanOrEqualTo(23).andAgeLessThanOrEqualTo(25);
        //查询年龄>=23 <=25
        StudentExample exp3 = new StudentExample();
        exp2.createCriteria().andAgeBetween(23,25);
        List<Student> students = studentMapper.selectByExample(exp1);
        List<Student> students1 = studentMapper.selectByExample(exp2);
        List<Student> students2 = studentMapper.selectByExample(exp3);
        System.out.println(students);
        System.out.println(students1);
        System.out.println(students2);
    }


    //删除
    @Test
    public void delete(){
        //根据主键删除
        /*int i = studentMapper.deleteByPrimaryKey(10);
        System.out.println(i==1?"删除成功":"删除失败");
        sqlSession.commit();*/
        //根据条件删除 sname,age
        StudentExample exp = new StudentExample();
        exp.createCriteria().andSnameEqualTo("坏心情").andAgeEqualTo(99);
        int i = studentMapper.deleteByExample(exp);
        System.out.println(i==1?"删除成功":"删除失败");
        sqlSession.commit();
    }


    //修改
    @Test
    public void update(){
        //根据主键修改
        Student student = new Student();
        student.setSid(12);
        student.setSname("好心情");
        //不需要修改的值 也要封装进去不然会被null覆盖
        student.setAge(28);
        student.setCid(3);
        int i = studentMapper.updateByPrimaryKey(student);
        sqlSession.commit();
        System.out.println(i==1?"修改成功":"修改失败");
    }
    @Test
    public void update2(){
        //根据主键修改
        Student student = new Student();
        student.setSid(12);
        student.setSname("坏心情");
        //根据可选的条件修改
        int i = studentMapper.updateByPrimaryKeySelective(student);
        sqlSession.commit();
        System.out.println(i==1?"修改成功":"修改失败");
    }

    @Test
    public void update3(){
        //封装修改后的值
        Student student = new Student();
        student.setSname("坏心情");
        student.setSid(10);
        //封装修改条件
        StudentExample exp = new StudentExample();
        exp.createCriteria().andSnameEqualTo("好心情").andSidEqualTo(12);
        int i = studentMapper.updateByExampleSelective(student,exp);
        sqlSession.commit();
        System.out.println(i==1?"修改成功":"修改失败");
    }
    @Test
    public void update4(){
        //封装修改后的值
        Student student = new Student();
        student.setSname("好心情");
        //使用updateByExample 不用修改的值也必须写 不然会报错或被空值覆盖
        student.setSid(11);
        student.setAge(22);
        student.setCid(3);
        //封装修改条件
        StudentExample exp = new StudentExample();
        exp.createCriteria().andSnameEqualTo("坏心情");
        int i = studentMapper.updateByExample(student,exp);
        sqlSession.commit();
        System.out.println(i==1?"修改成功":"修改失败");
    }



    //新增
    @Test
    public void insert(){
        Student student = new Student();
        student.setSid(12);
        student.setSname("阿尤");
        student.setAge(26);
        student.setCid(3);
        int i = studentMapper.insert(student);
        sqlSession.commit();
        System.out.println(i==1?"新增成功":"新增失败");
    }
    //新增
    @Test
    public void insert2(){
        Student student = new Student();
        student.setSname("咪咪");
        int i = studentMapper.insertSelective(student);
        sqlSession.commit();
        System.out.println(i==1?"新增成功":"新增失败");
    }
}

LOG4J日志

log4j简介:(http://lpgging.apache.org)

1.log4j是Apache提供的一款记录日志的工具

2.log4j的API与实现是分开的,从而使应用程序开发人员可以清楚地知道他们可以使用哪些类和方法

3.log4j既可以将日志信息打印在控制台,也可以打印输出到一个日志文件中

4.log4j 可以定制日志的输出格式

5.log4j 可以定制日志级别

日志级别

FATAL
致命的,表示非常严重的错误,一般是系统错误

ERROR

错误,表示代码错误,比较严重

WARN

警告,不影响程序的运行,但是可能存在风险

INFO

信息,表示一个普通的输出信息

DEBUG

调试,表示程序员人为的一些调试信息

FATAL< ERROR<WARN<INFO< DEBUG

开发中推荐使用 DEBUG和INFO

Lombok使用

1.导入lombok jar包

2.在实体类上加@Data

3.下载lombok插件:注意没有插件的话 没用

4.重启idea或删除out文件

会自动生成Get and Set方法 equals方法 hashCode方法 tostring方法

如果在idea里的插件库中搜索不到Lombok,去以下地址下载离线安装

安装之后一定要重启idea,才会生效

下载对应idea版本的lombok插件:https://plugins.jetbrains.com/plugin/6317-lombok/versions

PageHelper分页插件

1.导入jar包(jsqlparser.jar,pagehelper.jar)容易报错

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <properties resource="db.properties"></properties>
    <typeAliases>
        <typeAlias type="com.mybatis.pojo.Users" alias="u"></typeAlias>
    </typeAliases>
    <!--加入分页插件-->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
        </plugin>
    </plugins>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <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>
        <!--扫描当前com.mybatis.mapper包下所有的mapper.xml文件-->
        <package name="com.mybatis.mapper"/>
    </mappers>
</configuration>
 //查所有
    @Test
    public void selAll(){
        int pageNum=3;//页码(从一开始)
        int rows=3;//每页显示的数量
        //设置页码和每页显示的条数一定要放在查询语句之前 否则不生效
        PageHelper.startPage(pageNum,rows);//等同于sql语句中的limit(0,3)
        List<Users> users = mapper.getUsers();
        //整理分页之后的数据
        PageInfo<Users> usersPageInfo = new PageInfo<>(users);
        //获取分页之后的数据
        List<Users> list1 = usersPageInfo.getList();
        System.out.println("分页后");
        for (Users user : list1) {
            System.out.println(user.getId()+"-"+user.getUname()+"-"+user.getPwd());
        }
        System.out.println("当前页码"+usersPageInfo.getPageNum());
        System.out.println("数据总数"+usersPageInfo.getTotal());
    }


http://www.kler.cn/news/312958.html

相关文章:

  • VIVADO IP核之FIR插值器多相滤波仿真
  • flutter遇到问题及解决方案
  • OpenCV库学习之cv2.VideoWriter(函数)
  • 一起对话式学习-机器学习03——模型评估与模型选择
  • Lingo求解器基本语法
  • 基于ExtendSim的 电子制造 仿真模型
  • mendelson AS2的使用和配置
  • ThreadLocal、InheritableThreadLocal、TransmittableThreadLocal 区别,使用场景 示例
  • 【busybox记录】【shell指令】kill
  • Idea开发Gradle多模块项目踩坑纪实之一:Java模块无法执行
  • 【sql】MySQL中去重处理的方法
  • SpringMVC 处理 Ajax 请求
  • 【204】C++的vector删除重复元素
  • Java 基础篇:环境搭建、基本语法与面向对象编程
  • 【JPCS出版】第二届应用统计、建模与先进算法国际学术会议(ASMA2024,9月27日-29)
  • 华为OD机试真题- MELON的难题-2024年OD统一考试(E卷)
  • Leetcode 416. 分割等和子集(Medium)
  • 【第34章】Spring Cloud之SkyWalking分布式日志
  • 程序人生-2024我的个人总结
  • VScode开发GD32移植(标准库通用),保姆级!!!!!!!
  • OCR两篇革命之作
  • C#区分值类型和引用类型【一文读懂】
  • 第十三周:机器学习笔记
  • 【开发语言】写程序的两大基本原则(PO和NT原则)
  • three.js 热力图
  • Android Perfetto 学习
  • 聚焦于 Web 性能指标 TTI
  • 2024新动态:低代码开发占领新常态市场
  • ImportError: DLL load failed while importing _ssl: 找不到指定的模块的解决方法
  • man 命令:查看命令的帮助信息