Mybatis注解CRUD之对象、变量、集合三种方式封装数据的实现
- 创建User对象
- 创建dao包UserDao.java
- 创建测试类TestAnnotation.java
- 注意:本人使用maven创建的项目
- pom.xml配置
- 建表语句:
- 数据库连接、映射mappers请自行配置.
创建User对象
public class User implements Serializable{
private Integer id;
private String username;
private String address;
private String sex;
private Date birthday;
//getter和setter和toString方法已略
}
创建dao包UserDao.java
public interface UserDao {
//传递2个条件或者多个条件,使用@Param注解获取传递数据的对应值,注意value的值必须有传递的参数名相同
@Select(value = "select * from user where username like #{name} and sex = #{sex}")
List findByNameAndSex(@Param(value = "name") String username,@Param(value = "sex") String sex);
//使用map集合传递值
@Select(value = "select * from user where username like #{name} and sex = #{sex}")
List findByNameAndSex2(Map map);
//使用user对象传递数据,直接在#{}中括号内写user对象的变量名
@Select(value = "select * from user where username like #{username} and sex = #{sex}")
List findByUserNameAndSex(User user);
}
创建测试类TestAnnotation.java
public class TestAnnotation{
InputStream is = null;
SqlSession sqlSession = null;
UserDao userDao = null;
AccountDao accountDao = null;
@Before
public void init() throws IOException {
// 1:加载配置文件和映射文件(读取InputStream)
is = Resources.getResourceAsStream("sqlMapConfig.xml");
// 2:获取SqlSessionFactory(相当于DataSource)
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
// 3:获取SqlSession(相当于Connection)
sqlSession = sqlSessionFactory.openSession();
// 4:使用SqlSession获取UserDao(代理)
userDao = sqlSession.getMapper(UserDao.class);
accountDao = sqlSession.getMapper(AccountDao.class);
}
@After
public void destory() throws IOException {
is.close();
//sqlSession.close();
sqlSession.commit();
}
@Test //方案1:使用变量传递参数
public void testfindByNameAndSex(){
String username = "%王%";
String sex = "女";
List list = userDao.findByNameAndSex(username, sex);
for (User user : list) {
System.out.println(user);
}
}
@Test //方案2:使用user对象传递参数
public void testByUserNameAndSex(){
String username = "%王%";
String sex = "女";
User user = new User();
user.setUsername(username);
user.setSex(sex);
List list = userDao.findByUserNameAndSex(user);
for (User user1 : list) {
System.out.println(user1);
}
}
@Test//方案3:使用map集合封装数据实现
public void findByMapNameAndSex(){
String username = "%王%";
String sex = "女";
Map map = new HashMap();
map.put("name",username);
map.put("sex",sex);
List list = userDao.findByNameAndSex2(map);
for (User user : list) {
System.out.println(user);
}
}
}
注意:本人使用maven创建的项目
pom.xml配置
org.mybatis
mybatis
3.4.5
mysql
mysql-connector-java
5.1.18
log4j
log4j
1.2.12
junit
junit
4.10
建表语句:
CREATE TABLE `user` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(32) NOT NULL COMMENT '用户名称',
`birthday` datetime default NULL COMMENT '生日',
`sex` char(1) default NULL COMMENT '性别',
`address` varchar(256) default NULL COMMENT '地址',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into `user`(`id`,`username`,`birthday`,`sex`,`address`) values (22,'老王','2018-02-27 17:47:08','男','北京'),(18,'二王','2018-03-02 15:09:37','女','北京金燕龙'),(17,'三王','2018-03-04 11:34:34','女','北京金燕龙'),(20,'传智播客','2018-03-04 12:04:06','男','北京金燕龙'),(18,'老王','2018-03-07 17:37:26','男','北京'),(18,'小马宝莉','2018-03-08 11:44:00','女','北京修正');
数据库连接、映射mappers请自行配置.