1.使用 mybatis 查询用户数据(读取用户列表)
2.使用 mybatis 增加用户数据
3.使用 mybatis 更新用户数据
4.使用 mybatis 删除用户数据
查询数据,前面已经讲过简单的查询单个用户数据,在这里将查询出用户列表,
要查询出列表,也就是返回 List, 在我们这个例子中也就是List , 要以这种方式返回数据,需要在User.xml里面配置返回的类型 resultMap, 注意不是 resultType, 而这个resultMap 所对应的应该是我们自己配置。
在此示例中,我们需要使用到以下表:
接下我们创建一个表:user,并插入一条记录信息,其结构如下所示:
CREATE TABLE `user` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(64) NOT NULL DEFAULT '',
`dept` varchar(254) NOT NULL DEFAULT '',
`website` varchar(254) DEFAULT '',
`phone` varchar(16) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', 'gaga', 'Tech', 'http://www.gaga.com', '13800009988');
我们首先来创建一个工程:mybatis-curd-03,与第一节中介绍的环境配置一样,加入所需的 jar 包:mysql-connector 和 mybatis3.jar。配置 conf.xml,其文件内容如下
注意:需要引入数据表的model:
2、创建 Java 类和接口
创建数据表的mode的java类,和相应的增删改查接口类以及实现方法类(配置)
数据表model类:
package me.gacl.domain;
public class User {
private int id;
private String name;
private String dept;
private String phone;
private String website;
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website = website;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", dept=" + dept + ", phone=" + phone + ", website=" + website
+ "]";
}
}
增删改查的接口类
IUser.java接口位于包me.gacl.dao 下,IUser.java接口代码内容如下:
package me.gacl.dao;
import java.util.List;
import me.gacl.domain.User;
public interface IUser {
public List getUserList();
public void insertUser(User user);
public void updateUser(User user);
public void deleteUser(int userId);
public User getUser(int id);
}
实现IUser.java增删改查类的配置方法
这里还需要一个XML文件,与前一小节中一样,使用的是 UserMapper.xml,在这我们分别对应了增删改查的操作(每一个操作的 ID 对应于IUser接口的方法),其内容如下:
INSERT INTO USER (name, dept, website, phone) VALUES (#{name}, #{dept}, #{website}, #{phone})
UPDATE USER SET name = #{name}, dept = #{dept}, website = #{website}, phone = #{phone} where id = #{id}
delete from user where id = #{id}
测试主程序:
package me.gacl.test;
import java.io.Reader;
import java.util.List;
import java.text.MessageFormat;
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 me.gacl.dao.IUser;
import me.gacl.domain.User;
public class Test3 {
private static SqlSessionFactory sqlSessionFactory;
private static Reader reader;
static {
try {
reader = Resources.getResourceAsReader("conf.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}catch(Exception e)
{
e.printStackTrace();
}
}
public static SqlSessionFactory getSession()
{
return sqlSessionFactory;
}
public static void main(String[] args)
{
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
//用户数据列表
//getUserList();
//插入数据
//insertUser();
//修改数据
//updateUser();
//删除数据
deleteUser();
}finally {
sqlSession.close();
}
}
public static void getUserList()
{
try {
SqlSession sqlSession = sqlSessionFactory.openSession();
IUser iuser = sqlSession.getMapper(IUser.class);
//显示USER信息
System.out.println("Test get start....");
printUsers(iuser.getUserList());
System.out.println("Test get finishing");
}catch(Exception e)
{
e.printStackTrace();
}
}
public static void printUsers(final List users)
{
int count = 0;
for(User user: users)
{
System.out.println( MessageFormat.format( "======User[{0}]======", ++count) );
System.out.println("User id:" + user.getId());
System.out.println("User name:"+user.getName());
System.out.println("User dept:"+user.getDept());
System.out.println("User website:"+user.getWebsite());
}
}
public static void insertUser()
{
try {
//连接sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
//获取mapper
IUser iuser = sqlSession.getMapper(IUser.class);
System.out.println("Test insert start......");
//执行插入
User user = new User();
user.setId(0);
user.setName("google");
user.setDept("tech");
user.setPhone("13888888889");
user.setWebsite("http://www.google.com");
iuser.insertUser(user);
//提交事务
sqlSession.commit();
System.out.println("Test insert end......");
//插入后显示
System.out.println("alter insert");
getUserList();
System.out.println("--------------------------------");
}catch(Exception e)
{
e.printStackTrace();
}
}
public static void updateUser()
{
try {
//连接session
SqlSession sqlSession = sqlSessionFactory.openSession();
IUser iuser = sqlSession.getMapper(IUser.class);
System.out.println("Test update start......");
System.out.println(iuser.getUserList());
System.out.println("-----------------------");
//执行更新
User user = iuser.getUser(1);
user.setName("gaga");
iuser.updateUser(user);
//提交事务
sqlSession.commit();
System.out.println("-----------------------");
System.out.println(iuser.getUserList());
System.out.println("Test update end......");
}catch(Exception e)
{
e.printStackTrace();
}
}
public static void deleteUser()
{
try {
//连接sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
IUser iUser = sqlSession.getMapper(IUser.class);
System.out.println("Test delete start......");
//显示删除前的
System.out.println("befor delete");
printUsers(iUser.getUserList());
iUser.deleteUser(1);
//提交数据
sqlSession.commit();
//显示删除后的
System.out.println(" alter delete ");
printUsers(iUser.getUserList());
System.out.println("Test delete end......");
}catch(Exception e)
{
e.printStackTrace();
}
}