2.mybatis基于接口开发

上一篇对mybatis进行了简单的介绍并且进行了HelloWorld开发,在案例中使用的mybatis xml配置形式开发的,但在实际使用中基本是采用接口形式开发的,下面基于接口形式的开发提供案例说明。

1.创建数据表结果和实体类

数据表结果:

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(255) DEFAULT NULL,
  `user_password` varchar(255) DEFAULT NULL,
  `user_email` varchar(255) DEFAULT NULL,
  `user_info` varchar(255) DEFAULT NULL,
  `head_img` varchar(255) DEFAULT NULL,
  `create_time` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;

实体类:

public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    private Integer id;

    private String userName;

    private String userPassword;

    private String userEmail;

    private String userInfo;

    private String headImg;

    private Date createTime;


    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 getUserPassword() {
        return userPassword;
    }

    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }

    public String getUserEmail() {
        return userEmail;
    }

    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }

    public String getUserInfo() {
        return userInfo;
    }

    public void setUserInfo(String userInfo) {
        this.userInfo = userInfo;
    }

    public String getHeadImg() {
        return headImg;
    }

    public void setHeadImg(String headImg) {
        this.headImg = headImg;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    @Override
    public String toString() {
        return "User{" +
        "id=" + id +
        ", userName=" + userName +
        ", userPassword=" + userPassword +
        ", userEmail=" + userEmail +
        ", userInfo=" + userInfo +
        ", headImg=" + headImg +
        ", createTime=" + createTime +
        "}";
    }
}

2.创建Mapper接口

public interface UserMapper {
    User selectById(Long id);
}

3.在核心配置文件Mybatis-Config.xml中配置接口位置

注意相应映射文件也要放在这个路径下面。

    
        
    
    

4.配置映射文件UserMapping






    
        

        
        
        
        
        
    
    

5、测试

public class Demo1Test {
    private static SqlSessionFactory sqlSessionFactory;
    @BeforeClass
    public static void init() {
        try {
            Reader reader=Resources.getResourceAsReader("Mybatis-Config.xml");
            sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Test
    public void userTest() {
        SqlSession session=sqlSessionFactory.openSession();
        try {
          //回去UserMapper代理接口
            UserMapper userMapper=session.getMapper(UserMapper.class);
            User user = userMapper.selectById(1l);
            System.out.println(user);
        } catch (Exception e) {
            // TODO: handle exception
        }
        finally {
            session.close();
        }
    }
    }

你可能感兴趣的:(2.mybatis基于接口开发)