Mybatis入门程序

Mybatis入门程序_第1张图片
2016-11-18_235500.png

SqlMapConfig.xml(全局配置文件):




    
    
        
        
            
        
            
                
                
                
                
            
        
    
    
    
        
    

User.xml(映射配置文件):




    
    
    
    
    
    
    
    
        
        
            select LAST_INSERT_ID()
        
        insert into user (username,birthday,sex,address) values (#{username},#{birthday},#{sex},#{address})
        
         
    
    
    
    
        delete from user where id=#{id}
    
    
    
    
        update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id = #{id}
    


User.java

package cn.ztc.mybatis.po;

import java.util.Date;

public class User {
    
    private int id;
    private String username;
    private String sex;
    private Date birthday;
    private String address;
    
    public int getId() {
        return id;
    }
    public void setId(int 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;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    
    @Override
    public String toString() {
        return "User [id=" + id + ", username=" + username + "]";
    }
    
}

MybatisFirst.java

package cn.ztc.mybatis.first;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import cn.ztc.mybatis.po.User;

public class MybatisFirst {

    @Test
    public void findUserByIdTest() throws IOException{
        String resource = "SqlMapConfig.xml";
        //得到配置文件流
        InputStream inputStream = Resources.getResourceAsStream(resource);
        //创建会化工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //通过sqlSession操作数据库
        //第一个参数:映射文件中的[namespace].[statement id]
        //第二个参数:映射文件中匹配的resultType类型的对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //selectOne:查一条记录
        User user = sqlSession.selectOne("test.findUserById", 1);
        
        System.out.println(user);
        
        sqlSession.close();
    }
    
    @Test
    public void findUserByNameTest() throws IOException {
        String resource = "SqlMapConfig.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List list = sqlSession.selectList("test.findUserByName", "小明");
        System.out.println(list);
        sqlSession.close();
    }
    
    @Test
    public void insertUserTest() throws IOException {
        String resource = "SqlMapConfig.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        User user = new User();
        user.setUsername("王小军");
        user.setBirthday(new Date());
        user.setSex("1");
        user.setAddress("上海");
        sqlSession.insert("test.insertUser", user);
        
        sqlSession.commit();
        System.out.println(user.getId());
        sqlSession.close();
    }
    
    @Test
    public void deleteUserTest() throws IOException {
        String resource = "SqlMapConfig.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        
        sqlSession.delete("test.deleteUser", 27);
        
        sqlSession.commit();
        sqlSession.close();
    }
    
    @Test
    public void updateUserTest() throws IOException {
        String resource = "SqlMapConfig.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        User user = new User();
        user.setId(24);
        user.setUsername("张四丰");
        user.setBirthday(new Date());
        user.setSex("1");
        user.setAddress("上海");
        sqlSession.update("test.updateUser", user);
        
        sqlSession.commit();
        sqlSession.close();
    }

}

你可能感兴趣的:(Mybatis入门程序)