Mapper接口整合

四、Mybatis 学习

2、Mapper接口整合

mybatis-config.xml 配置





	
		
		
			
			
			
			
		
	


	


pojo 类

public class User {

private Integer id;

private String name;

private String password;

private Integer age;

private String address;

public User() {
	super();
}

public User(Integer id, String name, String password, Integer age, String address) {
	super();
	this.id = id;
	this.name = name;
	this.password = password;
	this.age = age;
	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 getPassword() {
	return password;
}

public void setPassword(String password) {
	this.password = password;
}

public Integer getAge() {
	return age;
}

public void setAge(Integer age) {
	this.age = age;
}

public String getAddress() {
	return address;
}

public void setAddress(String address) {
	this.address = address;
}

[@Override](https://my.oschina.net/u/1162528)
public String toString() {
	return "User [id=" + id + ", name=" + name + ", password=" + password + ", age=" + age + ", address=" + address
			+ "]";
}
}

IUserDao 接口

import java.util.List;

import com.tcwong.pojo.User;

public interface IUserDao {

List query();

int insert(User user);

int update(User user);

int delete(Integer id);
}

UserMapper.xml 配置



 
		

	insert into user (name,password,age,address) values(#{name},#{password},#{age},#{address})


	update user set name=#{name},password=#{password},age=#{age},address=#{address} where id=#{id}


	delete from user where id=#{id}

  

Utils类

public class Utils {

public static SqlSession getSession(){
	try {
		InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
		SqlSession openSession = sqlSessionFactory.openSession();
		return openSession;
	} catch (IOException e) {
		e.printStackTrace();
	}
	
	return null;
}
}

编写测试类

public class Test {

private SqlSession sqlSession = Utils.getSession();
private IUserDao mapper = sqlSession.getMapper(IUserDao.class);


/**
 * 查询
 */
@org.junit.Test
public void query(){
	List list = mapper.query();
	list.forEach((t)->System.out.println(t));
	sqlSession.close();
}

/**
 * 插入
 */
@org.junit.Test
public void insert(){
	User user = new User();
	user.setName("susu");
	user.setPassword("123456");
	user.setAge(16);
	user.setAddress("上海");
	int num = mapper.insert(user);
	System.err.println(num);
	sqlSession.commit();
	sqlSession.close();
}

/**
 * 更新
 */
@org.junit.Test
public void update(){
	User user = new User();
	user.setId(1008);
	user.setName("susu");
	user.setPassword("123456");
	user.setAge(16);
	user.setAddress("上海");
	int num = mapper.update(user);
	System.err.println(num);
	sqlSession.commit();
	sqlSession.close();
}

/**
 * 删除
 */
@org.junit.Test
public void delete(){
	int num = mapper.delete(1009);
	System.out.println(num);
	sqlSession.commit();
	sqlSession.close();
}
}

控制台输出

Mapper接口整合_第1张图片

进一步代码优化

mybatis-config.xml 配置








  
  
  
  
  
  
  
  
  
  
  
  
  
  
  



	



	



	
		
		
			
			
			
			
		
	



	
	


db.properties配置

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/demo?characterEncoding=utf-8
username=root
password=123456 

IUserDao 接口

public interface IUserDao {

List query();

int insert(User user);

int update(User user);

int delete(Integer id);
}

IUserDao.xml 配置




  
		

	insert into user (name,password,age,address) values(#{name},#{password},#{age},#{address})


	update user set name=#{name},password=#{password},age=#{age},address=#{address} where id=#{id}


	delete from user where id=#{id}

  

pojo 类

public class User {
private Integer id;

private String name;

private String password;

private Integer age;

private String address;

public User() {
	super();
}

public User(Integer id, String name, String password, Integer age, String address) {
	super();
	this.id = id;
	this.name = name;
	this.password = password;
	this.age = age;
	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 getPassword() {
	return password;
}

public void setPassword(String password) {
	this.password = password;
}

public Integer getAge() {
	return age;
}

public void setAge(Integer age) {
	this.age = age;
}

public String getAddress() {
	return address;
}

public void setAddress(String address) {
	this.address = address;
}

@Override
public String toString() {
	return "User [id=" + id + ", name=" + name + ", password=" + password + ", age=" + age + ", address=" + address
			+ "]";
}
}

utils 类

public class Utils {

public static SqlSession getSession(){
	try {
		InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
		SqlSession openSession = sqlSessionFactory.openSession();
		return openSession;
	} catch (IOException e) {
		e.printStackTrace();
	}
	
	return null;
}
}

编写测试类

public class Test {

private SqlSession sqlSession = Utils.getSession();
private IUserDao mapper = sqlSession.getMapper(IUserDao.class);


/**
 * 查询
 */
@org.junit.Test
public void query(){
	List list = mapper.query();
	list.forEach((t)->System.out.println(t));
	sqlSession.close();
}

/**
 * 插入
 */
@org.junit.Test
public void insert(){
	User user = new User();
	user.setName("susu");
	user.setPassword("123456");
	user.setAge(16);
	user.setAddress("上海");
	int num = mapper.insert(user);
	System.err.println(num);
	sqlSession.commit();
	sqlSession.close();
}

/**
 * 更新
 */
@org.junit.Test
public void update(){
	User user = new User();
	user.setId(1008);
	user.setName("susu");
	user.setPassword("123456");
	user.setAge(16);
	user.setAddress("上海");
	int num = mapper.update(user);
	System.err.println(num);
	sqlSession.commit();
	sqlSession.close();
}

/**
 * 删除
 */
@org.junit.Test
public void delete(){
	int num = mapper.delete(1009);
	System.out.println(num);
	sqlSession.commit();
	sqlSession.close();
}
}

控制台输出

Mapper接口整合_第2张图片

自定义类型转换器

ListStringTypeHandler 类

@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes(List.class)
public class ListStringTypeHandler extends 	BaseTypeHandler> {

@Override
public List getNullableResult(ResultSet arg0, String arg1) throws SQLException {
	return Arrays.asList(arg0.getString(arg1).split(";"));
}

@Override
public List getNullableResult(ResultSet arg0, int arg1) throws SQLException {
	return Arrays.asList(arg0.getString(arg1).split(";"));
}

@Override
public List getNullableResult(CallableStatement arg0, int arg1) throws SQLException {
	//调用存储过程的情况
	return null;
}


/**
 * PreparedStatement ps = conn.createPreparedStatement(sql);
 * ps.setObject(1,xxx); ps.setObject(2,xxx); ps.setObject(3,xxx); ....
 * ps.setObject(N,xxx);
 * 
 * 将数据保存到数据库中的方法 a b c a-b-c a;b;c
 */
@Override
public void setNonNullParameter(PreparedStatement ps, int i, List parameter, JdbcType jdbcType)
		throws SQLException {
	//处理自定义的类型数据
	StringBuilder sb = new StringBuilder();
	for (String s : parameter) {
		sb.append(s).append(";");
	}
	String msg = sb.toString();
	if(msg.contains(";")){
		msg = msg.substring(0, msg.lastIndexOf(";"));
	}
	//设置占位符对应的参数
	ps.setString(i, msg);
}
}

pojo 类

public class Student {

private Integer id;

private String name;

private List games;

public Student() {
	super();
}

public Student(Integer id, String name, List games) {
	super();
	this.id = id;
	this.name = name;
	this.games = games;
}

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 List getGames() {
	return games;
}

public void setGames(List games) {
	this.games = games;
}

@Override
public String toString() {
	return "Student [id=" + id + ", name=" + name + ", games=" + games + "]";
}


}

IStudentDao类

public interface IStudentDao {

List query();

int insert(Student student);

int update(Student student);

int delete(Student student);

}

IStudentDao.xml 配置




 
		

	insert into student (name,games) values(#{name},#{games})


	update student set name=#{name},games=#{games} where id=#{id}


	delete from student where id=#{id}

  

编写测试类

public class StudentTest {

private SqlSession sqlSession = Utils.getSession();
private IStudentDao mapper = sqlSession.getMapper(IStudentDao.class);

@Test
public void testQuery() {
	List list = mapper.query();
	list.forEach((t)->System.out.println(t));
	sqlSession.close();
}

@Test
public void testInsert() {
	Student student = new Student();
	student.setName("kang");
	List list = new ArrayList<>();
	list.add("a");
	list.add("b");
	list.add("c");
	student.setGames(list);
	int num = mapper.insert(student);
	System.out.println(num);
	sqlSession.commit();
	sqlSession.close();
	
}

@Test
public void testUpdate() {
}

@Test
public void testDelete() {
}
}

控制台打印

Mapper接口整合_第3张图片

注意事项

  • 配置文件中过包扫描去加载mapper文件,这个时候对mapper文件名有要求,要求mapper文件名和mapper接口必须一致。

  • 映射文件中,id必须和接口声明的方法一致

转载于:https://my.oschina.net/tcwong/blog/3042547

你可能感兴趣的:(Mapper接口整合)