使用mybatis动态代理Dao实现CRUD操作要求:
1、持久层接口(UserDao)和持久层接口的映射配置(UserDao.xml)必须在相同的包下
2、持久层映射配置中mapper标签的namespace属性取值必须是持久层接口的全限定类名
3、SQL语句的配置标签
1、创建动态代理Dao接口UserMapper
UserMapper.java
package com.sswblog.dao;
import com.sswblog.domain.User;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* @ Author :ShaoWei Sun.
* @ Date :Created in 19:55 2018/11/5
*/
public interface UserMapper {
/**
* 根据id查询用户
*
* @param id
* @return
*/
public User findUserById(Long id);
/**
* 查询所有用户
*
* @return
*/
public List findAllUsers();
/**
* 保存用户
*
* @param user
*/
public void insertUser(User user);
/**
* 根据id删除用户
*
* @param id
*/
public void deleteUser(Long id);
/**
* 更新用户
*
* @param user
*/
public void updateUser(User user);
/**
* 登录
* 使用动态代理,当接口中的某个方法有多个参数时,通过@Param注解指定参数名字
* @param userName
* @param password
* @return
*/
public User login(@Param("userName")String userName, @Param("password")String password);
}
2、创建映射文件UserMapper.xml(在核心配置文件中添加映射)
UserMapper.xml
INSERT INTO tb_user (
user_name,
password,
name,
age,
sex,
birthday,
created,
updated
)
VALUES
(
#{userName},
#{password},
#{name},
#{age},
#{sex},
#{birthday},
NOW(),
NOW()
)
UPDATE tb_user set
user_name = #{userName},
password = #{password},
name = #{name},
age = #{age},
sex = #{sex},
birthday = #{birthday},
updated = NOW()
WHERE
id = #{id}
delete from tb_user where
id = #{id}
注意:mapper中的nameSpace是持久层接口全限定名称
SQL语句的配置标签
mybatis-config.xml
CommonSql.xml映射文件
id,user_name,password,name,age,sex,birthday,created,updated
3、创建单元测试类UserMapperTest.java
package com.sswblog.test;
import com.sswblog.dao.UserMapper;
import com.sswblog.domain.User;
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.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
/**
* @ Author :ShaoWei Sun.
* @ Date :Created in 19:57 2018/11/5
*/
public class UserMapperTest {
private UserMapper userMapper = null;
private SqlSession sqlSession = null;
@Before
public void setup() throws IOException {
//读取mybatis配置文件
InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
sqlSession = sqlSessionFactory.openSession(true);//true为自动提交事务
userMapper = sqlSession.getMapper(UserMapper.class);
}
@Test
public void findUserByIds(){
User user = userMapper.findUserById(2L);
System.out.println("user = " + user);
}
@Test
public void findAllusers(){
List allUsers = userMapper.findAllUsers();
for (User allUser : allUsers) {
System.out.println("allUser = " + allUser);
}
}
@Test
public void insertUsers(){
User user = new User();
user.setUserName("小王八e");
user.setPassword("11111111");
user.setName("库里33");
user.setSex(12);
user.setAge(222);
Date date = new Date();
user.setBirthday(date);
userMapper.insertUser(user);
//手动提交事务
//sqlSession.commit();
System.out.println("user=="+user);
}
@Test
public void deleteUser(){
userMapper.deleteUser(20L);
//sqlSession.commit();
}
@Test
public void updateUser(){
User user = userMapper.findUserById(9L);
user.setAge(22);
userMapper.updateUser(user);
//sqlSession.commit();
}
@Test
public void login(){
User user = userMapper.login("lisi", "123456");
if (user == null){
System.out.println("登录失败");
}else {
System.out.println("登录成功");
}
}
}
测试了最后一条