• Mybatis学习(二)


    直接上例子:

    1.项目结构:

    2.具体代码及配置

    User.java

    package com.mybatis.bean;
    
    public class User {
    
        private int id;
        private String name;
        private int age;
        
        public User(int id, String name, int age) {
            this.id = id;
            this.name = name;
            this.age = age;
        }
        public User(){}
        
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        
    }

     GetFacotry.java

    package com.mybatis.util;
    
    import java.io.IOException;
    import java.io.Reader;
    
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    
    public class GetFactory {
    
        public static SqlSessionFactory getFactory(){
            
            Reader reader= null;
            try {
                reader = Resources.getResourceAsReader("conf.xml");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            SqlSessionFactory sessionfactory = new SqlSessionFactoryBuilder().build(reader);
            
            return sessionfactory;
        }
    }

    配置文件 conf.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">
             </transactionManager>
             <dataSource type="POOLED">
                    <property name="driver" value="com.mysql.jdbc.Driver" />
                    <property name="url" value="jdbc:mysql://localhost:3306/Mybatis" />
                    <property name="username" value="root" />
                    <property name="password" value="123456" />
             </dataSource>
           </environment>
        </environments>
        <mappers>
            <mapper resource="User.xml"></mapper>
        </mappers>
    </configuration>

     配置文件 User.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.mybatis.bean.UserMapper">
        <select id="getUser" parameterType="int" resultType="com.mybatis.bean.User">
           select * from user where id=#{id}
        </select>
        <!--  
           CRUD操作
        -->
        <insert id="addUser" parameterType="com.mybatis.bean.User">
           insert into user (name,age) values (#{name},#{age});
        </insert>
        <update id="updateUser" parameterType="com.mybatis.bean.User">
           update user set name=#{name},age=#{age} where id=#{id}
        </update>
        <delete id="deleteUser" parameterType="int">
           delete from user where id=#{id}
        </delete>
        <select id="selectAllUsers" resultType="com.mybatis.bean.User">
           select * from user
        </select>
    </mapper>

    3.单元测试

    test.java

    package com.mybatis.service;
    
    import java.util.List;
    
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.junit.Test;
    
    import com.mybatis.bean.User;
    import com.mybatis.util.GetFactory;
    
    public class test {
        
        @Test
        public void UnitTest_ADD(){
            SqlSessionFactory factory = GetFactory.getFactory();
            SqlSession session = factory.openSession();
            
            String statment = "com.mybatis.bean.UserMapper.addUser";
            User user = new User(-1,"新增",32);
            int insert = session.insert(statment,user);
            session.commit();
            session.close();
            System.out.println(insert);
        }
        
        @Test
        public void UnitTest_UPDATE(){
            SqlSessionFactory factory = GetFactory.getFactory();
            SqlSession session = factory.openSession();
            
            String statment = "com.mybatis.bean.UserMapper.updateUser";
            User user = new User(1,"更新",22);
            int insert = session.update(statment,user);
            session.commit();
            session.close();
            System.out.println(insert);
            
        }
        
        @Test
        public void UnitTest_DELETE(){
            SqlSessionFactory factory = GetFactory.getFactory();
            SqlSession session = factory.openSession();
            
            String statment = "com.mybatis.bean.UserMapper.deleteUser";
            int delete = session.delete(statment, 2);
            session.commit();
            session.close();
            System.out.println(delete);
            
        }
        
        @Test
        public void UnitTest_SELECT_ALL(){
            SqlSessionFactory factory = GetFactory.getFactory();
            SqlSession session = factory.openSession();
            
            String statment = "com.mybatis.bean.UserMapper.selectAllUsers";
            List<User> list= session.selectList(statment);
            session.close();
            System.out.println(list);
        }
    
    }
  • 相关阅读:
    vscode oom
    增加路由,改变请求出口网卡,改变请求目的IP
    autossh监听端口monitor port
    远程计划任务管理器(schtasks)
    《深入浅出MySQL++数据库开发优化与管理维护》 PDF 下载
    鸟哥的LINUX私房菜_基础学习篇(第三版) PDF 下载
    《Spark快速大数据分析》 PDF 下载
    《Hadoop全权威指南第三版(原版)》 PDF下载
    《NumPy学习指南(第2版)》 高清PDF 下载
    《利用Python进行数据分析》PDF下载
  • 原文地址:https://www.cnblogs.com/JsonShare/p/5218715.html
Copyright © 2020-2023  润新知