【MyBatis + Spring整合开发】(二)Dao式开发

复制上一节中的项目MyBatis_Spring,命名为MyBatis_Spring_DAO。

修改Mybatis主配置文件sqlMapConfig.xml。



  

    
    
        
    
    
    
        
        
        
    

创建com.test.bean包,包内新建实体类User.java,代码如下:

package com.test.bean;

import java.util.Date;

public class User {

    /**
     * CREATE TABLE `user` (
      `u_id` INT(11) NOT NULL AUTO_INCREMENT COMMENT '用户id',
      `u_username` VARCHAR(64) NOT NULL COMMENT '用户名',
      `u_password` VARCHAR(64) NOT NULL COMMENT '用户密码',
      `u_sex` VARCHAR(16) DEFAULT NULL COMMENT '用户性别',
      `u_createTime` DATETIME DEFAULT NULL COMMENT '用户创建时间',
      `u_cid` INT(11) NOT NULL COMMENT '用户国家id',
     */
    
    private Integer u_id;
    private String u_username;
    private String u_password;
    private String u_sex;
    private Date u_createTime;
    private Integer u_cid;
    
    public Integer getU_id() {
        return u_id;
    }
    public void setU_id(Integer u_id) {
        this.u_id = u_id;
    }
    public String getU_username() {
        return u_username;
    }
    public void setU_username(String u_username) {
        this.u_username = u_username;
    }
    public String getU_password() {
        return u_password;
    }
    public void setU_password(String u_password) {
        this.u_password = u_password;
    }
    public String getU_sex() {
        return u_sex;
    }
    public void setU_sex(String u_sex) {
        this.u_sex = u_sex;
    }
    public Date getU_createTime() {
        return u_createTime;
    }
    public void setU_createTime(Date u_createTime) {
        this.u_createTime = u_createTime;
    }
    public Integer getU_cid() {
        return u_cid;
    }
    public void setU_cid(Integer u_cid) {
        this.u_cid = u_cid;
    }
    @Override
    public String toString() {
        return "User [u_id=" + u_id + ", u_username=" + u_username + ", u_password=" + u_password + ", u_sex=" + u_sex
                + ", u_createTime=" + u_createTime + ", u_cid=" + u_cid + "]";
    }
    

}

创建com.test.bean包,包内新建实现类接口UserDAO.java和实现类UserDaoImpl.java,代码如下:
UserDAO.java:

package com.test.dao;

import com.test.bean.User;

public interface UserDao {
    
    //根据id查询用户
    public User getUserById(Integer id);
    
}

UserDaoImpl.java:

package com.test.dao;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import com.test.bean.User;

public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
   
    @Override
    public User getUserById(Integer id) {
        //从SqlSessionDaoSupport中获取一个sqlSession
        SqlSession session = getSqlSession();
        //操作数据库
        return session.selectOne("UserMapper.selectUserById", id);
    }

}

在Spring主配置文件applicationContext.xml中添加代码如下:

    
    
        
        
    

创建com.test.mapper包,包内新建UserMapper.java,代码如下:





    
    
    

创建com.test.test包,包内新建测试类UserDaoTest.java,代码如下:

package com.test.test;


import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.test.bean.User;
import com.test.dao.UserDaoImpl;

public class UserDaoTest {
    @Test
    public void DaoTest() {
        //spring管理对象
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserDaoImpl userDao = ac.getBean(UserDaoImpl.class);
        
        User user = userDao.getUserById(1);
        System.out.println(user);
    }
    
}
项目目录

进行测试。

测试结果

你可能感兴趣的:(【MyBatis + Spring整合开发】(二)Dao式开发)