一. Mybatis官方推荐, 接口动态代理开发
1. 动态代理dao开发规则
1.1. namespace必需是接口的全路径名。
1.2. 接口的方法名必需与sql id一致。
1.3. 接口的入参与parameterType类型一致。
1.4. 接口的返回值必须与resultType类型一致。
二. Mybatis动态dao开发例子
1. 新建一个名为MyBatisDao的Java工程, 拷入相关jar包
2. 新建User.java
package com.lywgames.domain;
import java.util.Date;
public class User {
private Integer id;
private String name;
private String sex;
private Date birthday;
private String address;
public User() { }
public User(String name, String sex, Date birthday, String address) {
this.name = name;
this.sex = sex;
this.birthday = birthday;
this.address = address;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", sex=" + sex + ", birthday=" + birthday + ", address=" + address
+ "]";
}
}
3. 新建UserDao.java数据库操作接口
package com.lywgames.dao;
import java.util.List;
import com.lywgames.domain.User;
public interface UserDao {
public List<User> selectUser(String name);
public int insertUser(User user);
public int updateUser(User user);
public int deleteUser(String name);
}
4. 在src目录下新建UserMapper.xml映射文件
5. 在src目录下新建SqlMapConfig.xml数据库配置文件
6. 新建Test.java
package com.lywgames;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
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 com.lywgames.dao.UserDao;
import com.lywgames.domain.User;
public class Test {
public static void main(String[] args) {
SqlSession sqlSession = null;
try {
// 指定全局配置文件
String resource = "SqlMapConfig.xml";
// 读取配置文件
InputStream inputStream = Resources.getResourceAsStream(resource);
// 构建sqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 获取sqlSession
sqlSession = sqlSessionFactory.openSession();
UserDao userDao = sqlSession.getMapper(UserDao.class);
// 插入操作
User insertUser = new User("李阳", "男", new Date(System.currentTimeMillis()), "河南省郑州市");
int insertResult = userDao.insertUser(insertUser);
System.out.println("插入操作影响行数:" + insertResult);
// 查询操作
List<User> users = userDao.selectUser("李");
for (User user : users) {
System.out.println(user);
}
// 更新操作
User user = new User(4, "李茹", "女", null, null);
int updateResult = userDao.updateUser(user);
System.out.println("更新操作影响行数:" + updateResult);
// 删除操作
int deleteResult = userDao.deleteUser("卫");
System.out.println("删除操作影响行数:" + deleteResult);
// 写操作, 需要提交事务。
sqlSession.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
if(sqlSession != null) {
// 释放资源
sqlSession.close();
}
}
}
}
7. 查看user表数据
8. 运行项目
9. 运行项目后查看user表数据