zoukankan      html  css  js  c++  java
  • JAVA系列笔记十三之Mybatis

    Mybatis官网地址:https://mybatis.org/

    1.添加依赖包

       <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.5.4</version>
            </dependency>

    2.主配置文件,目录在resources文件夹下

    <?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="mysql">
        <environment id="mysql">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql:///mybatis001"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
        <mappers>
            <mapper resource="mappers/UserMapper.xml" />
        </mappers>
    </configuration>

    主配置文件的文件属性的用法

     进一步简化,通过读取属性文件获取数据库链接字符串

     读取引用:

    <properties resource="jdbc.propeties"/>

     类别名的定义

    3. mapper配置文件

    <?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.xiaoyui.mapper.UserMapper">
     <!--id:对应的接口的函数,resultType:为返回值,-->
     <select id="GetAll"  resultType="com.xiaoyui.domain.UserInfo"> select * from user;</select>
     <!--形参有两种形式#{}或者${}-->
     <select id="GetFindId"  resultType="com.xiaoyui.domain.UserInfo" parameterType="java.lang.Integer">
      select * from user where id=#{id};</select>
     <!--#{username}类的属性名称-->
     <insert id="Add" parameterType="com.xiaoyui.domain.UserInfo">
      <!--插入操作的返回值-->
      <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
        select last_insert_id();
      </selectKey>
       insert into user(username,sex) values(#{username},#{sex});
     </insert>
     <update id="Update" parameterType="com.xiaoyui.domain.UserInfo">
        update user set  username=#{username},sex=#{sex} where id=#{id};
     </update>
     <delete id="Delete" parameterType="java.lang.Integer">
        delete from user where id=#{id};
     </delete>
    </mapper>

    4.代码

    bean

    public class UserInfo  implements   Serializable{
    
        private  Integer id;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getSex() {
            return sex;
        }
    
        public void setSex(String sex) {
            this.sex = sex;
        }
    
        private String username;
        private  String sex;
    }

     Dao接口:

    public interface UserMapper {
    
        public List<UserInfo> GetAll();
    
        public  UserInfo GetFindId(int id);
    
        public int Add(UserInfo user);
    
        public  int Update(UserInfo user);
    
        public  int Delete(int id);
    }

    测试代码如下:

      @Test
        public  void testUserGetAll() throws IOException
        {
    
            InputStream inputStream= Resources.getResourceAsStream("SqlMapConfig.xml");
            SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession session= sqlSessionFactory.openSession();
    
    
            UserMapper userMapper=session.getMapper(UserMapper.class);
            List<UserInfo> list= userMapper.GetAll();
            /*
            第二种获取实体对象的方式
             */
            //List<UserInfo> list= session.selectList("com.xiaoyui.mapper.UserMapper.GetAll");
            for (UserInfo user : list) {
                System.out.println(user.getUsername());
            }
            //获取单个
            UserInfo current=userMapper.GetFindId(1);
    
            //插入
            userMapper.Add(new UserInfo(){
            });
            //修改
            current.setUsername("test6");
            userMapper.Update(current);
            //删除
            userMapper.Delete(1);
            //提交事务
            session.commit();
    
    
            session.close();
            inputStream.close();
        }

     5.其他

     like的使用

     <!--自带百分号,模糊查询 %查%,预编译,防止sql注入-->
     <select id="find" parameterType="java.lang.String" resultType="com.xiaoyui.domain.UserInfo">
      select * from user like #{username}
     </select>
     <!--SQL拼接-->
     <select id="find" parameterType="java.lang.String" resultType="com.xiaoyui.domain.UserInfo">
      select * from user like '%${value}%'
     </select>

    resultMap:JavaBean与数据库字段的映射

    <resultMap id="userdto" type="com.xiaoyui.domain.UserInfo">
      <collection property="id" column="_id" />
     </resultMap>

     联合查询示例,多对一

     联合查询1对多

    6.#{}与${}区别

    引用类型的时候,两者表达一致。

    普通类型时,#是随意值,$必须是value

  • 相关阅读:
    安装jupyter
    git 查看分支图
    Docker原生健康检查使用
    压力测试指标判定
    Docker限制日志
    docker link 过时不再用了?那容器互联、服务发现怎么办?(2017年文章,建议使用docker network自定义网络)
    Nginx配置TCP服务负载均衡
    【转让】看看有你喜欢的书籍嘛?--都是我翻过的。
    《SOD框架企业级应用数据架构实战》新书简介和预定
    一年之计在于春,2015开篇:PDF.NET SOD Ver 5.1完全开源
  • 原文地址:https://www.cnblogs.com/junhe/p/12887995.html
Copyright © 2011-2022 走看看