ResultMap映射
适用场景:
当查询出来的字段名和对象中的属性名不一致的情况,就没办法使用resultType来默认映射(同名规则)
解决方案:
使用resultMap来映射数据库中的字段到底注入到对象中什么属性中.
具体操作
具体代码
mybatis.xml
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
usermapper.xml
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
INSERT INTO user (username,password,age)VALUES (#{username},#{password},#{age})
UPDATE user SET username=#{username},password=#{password},age=#{age} WHERE id=#{id}
SELECT *FROM user WHERE id=#{id}
SELECT *FROM user
DELETE FROM user WHERE id=#{id}
SELECT id AS "号数", username AS "用户名", age AS "年龄" FROM user
log4j.properties
# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.mybatis.domain.usermapper=TRACE //mybatis.domain.usermapper包名+文件名
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
db.properties
db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/mybatis
db.username=root
db.password=123456
user类
package mybatis.domain;
import lombok.Data;
public class user {
private Long id;
private String username;
private String password;
private Integer age;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
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;
}
@Override
public String toString() {
return "user{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", age=" + age +
'}';
}
}
mybatisutils工具类
package mybatis.utils;
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 java.io.IOException;
public class mybatisutils {
private SqlSessionFactory sf;
private static mybatisutils instance=new mybatisutils();
public mybatisutils(){
try {
sf=new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis.xml"));
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession opensession(){
return instance.sf.openSession();
}
}
usertest测试类
package mybatis.test;
import mybatis.domain.user;
import mybatis.domain.usermapper;
import mybatis.utils.mybatisutils;
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 org.junit.Test;
import java.io.IOException;
import java.util.List;
public class usertest {
@Test
public void test() throws IOException {
user u=new user();
u.setAge(17);
u.setUsername("小李子");
u.setPassword("123456");
SqlSessionFactory sf=new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis.xml"));
SqlSession session=mybatisutils.opensession();
//session.insert("mybatis.domain.usermapper.save",u);
usermapper mapper=session.getMapper(usermapper.class);
mapper.save(u);
session.commit();
session.close();
System.out.println(u);
}
@Test
public void testupdate(){
user u=new user();
u.setAge(47);
u.setUsername("李公公");
u.setPassword("123");
u.setId(4L);
SqlSession session=mybatisutils.opensession();
//session.update("mybatis.domain.usermapper.update",u);
usermapper mapper=session.getMapper(usermapper.class);
mapper.update(u);
session.commit();
session.close();
}
@Test
public void testget(){
SqlSession session=mybatisutils.opensession();
//user u=session.selectOne("mybatis.domain.usermapper.search",1L);
usermapper mapper=session.getMapper(usermapper.class);
user u=mapper.search(4L);
System.out.println(u);
session.commit();
session.close();
}
@Test
public void testlist(){
SqlSession session=mybatisutils.opensession();
//List
//for (user user : users) {
// System.out.println(user);
//}
usermapper mapper=session.getMapper(usermapper.class);
List
for (user user : list) {
System.out.println(user);
}
session.close();
}
@Test
public void testdelete(){
SqlSession session=mybatisutils.opensession();
//session.delete("mybatis.domain.usermapper.delete",7L);
usermapper mapper=session.getMapper(usermapper.class);
mapper.delete(12L);
session.commit();
session.close();
}
@Test
public void testresultlist(){
SqlSession session=mybatisutils.opensession();
//List
//for (user user : users) {
// System.out.println(user);
//}
usermapper mapper=session.getMapper(usermapper.class);
List
for (user user : resultlist) {
System.out.println(user);
}
}
}
package mybatis.domain;
import java.util.List;
public interface usermapper {
void save(user u);
void update(user u);
user search(Long id);
List
void delete(Long id);
List
}
测试结果如下