用户表
角色表
USE `woniu`; DROp TABLE IF EXISTS `woniu_user`; CREATE TABLE `woniu_user` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID', `userCode` varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '用户编码', `userName` varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '用户名称', `userPassword` varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '用户密码', `gender` int(10) DEFAULT NULL COMMENT '性别(1:女、 2:男)', `birthday` date DEFAULT NULL COMMENT '出生日期', `phone` varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '手机', `address` varchar(30) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '地址', `userRole` bigint(20) DEFAULT NULL COMMENT '用户角色(取自角色表-角色id)', `createdBy` bigint(20) DEFAULT NULL COMMENT '创建者(userId)', `creationDate` datetime DEFAULT NULL COMMENT '创建时间', `modifyBy` bigint(20) DEFAULT NULL COMMENT '更新者(userId)', `modifyDate` datetime DEFAULT NULL COMMENT '更新时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; insert into `woniu_user`(`id`,`userCode`,`userName`,`userPassword`,`gender`,`birthday`,`phone`,`address`,`userRole`,`createdBy`,`creationDate`,`modifyBy`,`modifyDate`) values (1,'admin','系统管理员','1234567',1,'1983-10-10','13688889999','北京市海淀区成府路207号',1,1,'2013-03-21 16:52:07',NULL,NULL),(2,'liming','李明','0000000',2,'1983-12-10','13688884457','北京市东城区前门东大街9号',2,1,'2014-12-31 19:52:09',NULL,NULL),(5,'hanlubiao','韩路彪','0000000',2,'1984-06-05','18567542321','北京市朝阳区北辰中心12号',2,1,'2014-12-31 19:52:09',NULL,NULL),(6,'zhanghua','张华','0000000',1,'1983-06-15','13544561111','北京市海淀区学院路61号',3,1,'2013-02-11 10:51:17',NULL,NULL),(7,'wangyang','王洋','0000000',2,'1982-12-31','13444561124','北京市海淀区西二旗辉煌国际16层',3,1,'2014-06-11 19:09:07',NULL,NULL),(8,'zhaoyan','赵燕','0000000',1,'1986-03-07','18098764545','北京市海淀区回龙观小区10号楼',3,1,'2016-04-21 13:54:07',NULL,NULL),(10,'sunlei','孙磊','0000000',2,'1981-01-04','13387676765','北京市朝阳区管庄新月小区12楼',3,1,'2015-05-06 10:52:07',NULL,NULL),(11,'sunxing','孙兴','0000000',2,'1978-03-12','13367890900','北京市朝阳区建国门南大街10号',3,1,'2016-11-09 16:51:17',NULL,NULL),(12,'zhangchen','张晨','0000000',1,'1986-03-28','18098765434','朝阳区管庄路口北柏林爱乐三期13号楼',3,1,'2016-08-09 05:52:37',1,'2016-04-14 14:15:36'),(13,'dengchao','邓超','0000000',2,'1981-11-04','13689674534','北京市海淀区北航家属院10号楼',3,1,'2016-07-11 08:02:47',NULL,NULL),(14,'yangguo','杨过','0000000',2,'1980-01-01','13388886623','北京市朝阳区北苑家园茉莉园20号楼',3,1,'2015-02-01 03:52:07',NULL,NULL),(15,'zhaomin','赵敏','0000000',1,'1987-12-04','18099897657','北京市昌平区天通苑3区12号楼',2,1,'2015-09-12 12:02:12',NULL,NULL); DROP TABLE IF EXISTS `woniu_role`; CREATE TABLE `woniu_role` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID', `roleCode` varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '角色编码', `roleName` varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '角色名称', `createdBy` bigint(20) DEFAULT NULL COMMENT '创建者', `creationDate` datetime DEFAULT NULL COMMENT '创建时间', `modifyBy` bigint(20) DEFAULT NULL COMMENT '修改者', `modifyDate` datetime DEFAULT NULL COMMENT '修改时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; insert into `woniu_role`(`id`,`roleCode`,`roleName`,`createdBy`,`creationDate`,`modifyBy`,`modifyDate`) values (1,'woniu_ADMIN','系统管理员',1,'2016-04-13 00:00:00',NULL,NULL),(2,'woniu_MANAGER','经理',1,'2016-04-13 00:00:00',NULL,NULL),(3,'woniu_EMPLOYEE','普通员工',1,'2016-04-13 00:00:00',NULL,NULL);二、准备实体类 User
public class User {
private Integer id; //id
private String userCode; //用户编码
private String userName; //用户名称
private String userPassword; //用户密码
private Integer gender; //性别
private Date birthday; //出生日期
private String phone; //电话
private String address; //地址
private Integer userRole; //用户角色
private Integer createdBy; //创建者
private Date creationDate; //创建时间
private Integer modifyBy; //更新者
private Date modifyDate; //更新时间
//省略get set方法
}
三、准备映射文件
核心配置文件中引入映射文件
四、CURD 1、查询用户表条数
方法
public interface UserMapper {
public int count();
}
sql
namespace绑定dao层接口的全路径:namespace的值必须全局唯一
当namespace绑定的是dao接口的全路径时,其实现数据持久化的方式为由代理模式实现数据持久化。
即会自动产生代理,自动实现数据的持久化,不需要实现dao层的接口
测试
@Test
public void testCount() {
//創建sqlSession
SqlSession sqlSession = MyBatisUtil.createSqlSession();
//使用sqlSession生成UserDao的代理對象
UserDao userDao = sqlSession.getMapper(UserDao.class);
//調用方法
int count = userDao.count();
//輸出
System.out.println(count);
//關閉連接
sqlSession.close();
}
每次执行都需要打开连接,关闭连接,代码重复。可以使用Before和After注解
@Before//測試前獲取連接
public void init() {
sqlSession = MyBatisUtil.createSqlSession();
}
@After//測試后關閉連接
public void destory() {
sqlSession.close();
}
@Test
public void testCount() {
//使用sqlSession生成UserDao的代理對象
UserDao userDao = sqlSession.getMapper(UserDao.class);
//調用方法
int count = userDao.count();
//輸出
System.out.println(count);
}
2、根据id查询用户
方法
public User getUserById(int userId);
SQL
测试
@Test
public void testGetUesrById() {
User user = sqlSession.getMapper(UserDao.class).getUserById(1);
System.out.println(user);
}
3、查询所有用户数据
方法
ListfindAllUser();
SQL
测试
@Test
public void testFindAllUser() {
sqlSession.getMapper(UserDao.class).findAllUser().forEach(System.out::println);
}
4、添加用户
方法
int addUser(User user);
SQL
insert into woniu_user(userCode,userName) values(#{userCode},#{userName})
测试
@Test
public void testAddUser() {
User user = new User();
user.setUserName("abc");
user.setUserCode("abc");
sqlSession.getMapper(UserDao.class).addUser(user );
}
5、修改用户
方法
int updateUser(User user);
SQL
update woniu_user set userCode=#{userCode},userName=#{userName} where id = #{id}
测试
@Test
public void testUpdateUser() {
User user = new User();
user.setUserName("陳老師");
user.setUserCode("teacher");
user.setId(2);
sqlSession.getMapper(UserDao.class).updateUser(user );
}
6、删除用户
方法
int deleteUser(int userId);
SQL
delete from woniu_user where id = #{id}
测试
@Test
public void testDeleteUser() {
sqlSession.getMapper(UserDao.class).deleteUser(15);
}
7、配置别名
在mybatis-config.xml中加入以下代码
五、常见报错 1、接口未注册 2、映射文件中的id重复了 3、方法在mapper映射文件中没有绑定 4、参数为实体类时 没有对应的属性 5、对应的参数找不到 6、返回的结果太多



