mybatis之快速搭建(动态代理)

目录

 

 

 

导入坐标

创建SqlMapConfig.xml主配置文件,导入约束,配置连接的数据库,连接池,别名,缓存等

创建子配置映射文件UserDao.xml文件,导入约束,书写sql

接口UserDao.java

 实体类User.java:

log4j.properties配置文件:

快速搭建的mybatis项目目录结构:

junit测试类编写

测试结果:通过


 

 

导入坐标


        
            org.mybatis
            mybatis
            3.4.6
        

        
            mysql
            mysql-connector-java
            5.1.47
        

        
            junit
            junit
            4.12
        

        
            log4j
            log4j
            1.2.12
        
    

创建SqlMapConfig.xml主配置文件,导入约束,配置连接的数据库,连接池,别名,缓存等






    
        
    

    
        
            
            
                
                
                
                
            
        
    

    
        
    

创建子配置映射文件UserDao.xml文件,导入约束,书写sql








    

    

    

    
        
            select  last_insert_id();
        
        insert into user(name,money) values (#{name},#{money})
    

    
        update user set name=#{name},money=#{money} where id=#{id}
    

    
        delete from user where id=#{id}
    

 

接口UserDao.java

/**
 * @Date 2019/9/14 16:59
 * by mocar
 */
public interface UserDao {
    List findAll();

    User findById(Integer id);

    List findByName(String name);

    void saveUser(User user);

    void updateUser(User user);

    void deleteUser(Integer id);
}

 

 实体类User.java:

/**
 * @Date 2019/9/14 16:53
 * by mocar
 */
public class User implements Serializable {

    private Integer id;
    private String name;
    private Float money;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Float getMoney() {
        return money;
    }

    public void setMoney(Float money) {
        this.money = money;
    }
}

 

log4j.properties配置文件:

# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE            debug   info   warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE

# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=d:\axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

 

快速搭建的mybatis项目目录结构:

mybatis之快速搭建(动态代理)_第1张图片

 

junit测试类编写

/**
 * @Date 2019/9/14 17:19
 * by mocar
 */
public class TestMybatis {
    private InputStream resourceAsStream=null;
    private UserDao userDao=null;
    private SqlSession session=null;
    @Before
    public void before() throws IOException {
        resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sessionFactory = builder.build(resourceAsStream);
        session = sessionFactory.openSession(false);//开启事务
        userDao = session.getMapper(UserDao.class);
    }
    @After
    public void after(){
        session.commit();
        if (session!=null){
            session.close();
        }
        if (resourceAsStream!=null){
            try {
                resourceAsStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void findAll() {
        List userList = userDao.findAll();
        for (User user : userList) {
            System.out.println(user);
        }

    }

    @Test
    public void findById() {
        User user = userDao.findById(1);
        System.out.println(user);

    }

    @Test
    public void findByName() {
        List userList = userDao.findByName("a");
        for (User user : userList) {
            System.out.println(user);
        }
    }

    @Test
    public void saveUser() {
        User user = new User();
        user.setName("zhangsan");
        user.setMoney(122f);
        userDao.saveUser(user);
        System.out.println(user);

    }

    @Test
    public void updateUser() {
        User user = userDao.findById(3);
        user.setName("jerry");
        user.setMoney(user.getMoney()+1000f);
        userDao.updateUser(user);

    }

    @Test
    public void deleteUser() {
        userDao.deleteUser(3);

    }




}

测试结果:通过

mybatis之快速搭建(动态代理)_第2张图片

插入数据,返回自增主键

mybatis之快速搭建(动态代理)_第3张图片

 

你可能感兴趣的:(mybatis)