4、Mybatis中实体类bean的属性名与数据库表中对应字段名不同

例如一个USER表中一个字段为name,在工程中创建的USER表对应的bean为User,其中一个属性为user_name,在Mybaits中的sql映射文件中一定要映射字段,否则会操作sql不正确。下面以两种方式为示例:
表如下:
4、Mybatis中实体类bean的属性名与数据库表中对应字段名不同_第1张图片

一、在sql语句中进行映射

1、创建表对应的实体类User,类中的属性名与表USER中的字段名不一致。

package com.lzj.mybaits.test1;

public class User {
    private int user_id;
    private String user_name;
    private int user_age;

    public User() {
        super();
    }

    public User(int user_id, String user_name, int user_age) {
        super();
        this.user_id = user_id;
        this.user_name = user_name;
        this.user_age = user_age;
    }

    public int getUser_id() {
        return user_id;
    }

    public void setUser_id(int user_id) {
        this.user_id = user_id;
    }

    public String getUser_name() {
        return user_name;
    }

    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }

    public int getUser_age() {
        return user_age;
    }

    public void setUser_age(int user_age) {
        this.user_age = user_age;
    }

    @Override
    public String toString() {
        return "User [user_id=" + user_id + ", user_name=" + user_name + ", user_age=" + user_age + "]";
    }

}

2、以查询语句为例,定义sql的映射userMapper.xml文件




<mapper namespace="com.lzj.mybaits.test1.userMapper">

    
    <select id="getUser" parameterType="int" resultType="User">
        
        select id user_id, name user_name, age user_age
        from users where id=#{id}
    select>

mapper>

3、定义Mybaits的配置文件conf.xml



<configuration>

    <typeAliases>
        <package name="com.lzj.mybaits.test1"/>
    typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/lzj_database" />
                <property name="username" value="root" />
                <property name="password" value="lzjlzj" />
            dataSource>
        environment>
    environments>

    <mappers>
        <mapper resource="com/lzj/mybaits/test1/userMapper.xml"/>
    mappers>

configuration>

4、定义测试类

package com.lzj.mybaits.test1;
import java.io.InputStream;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MybaitsTest {

    public static void main(String[] args) throws Exception {
        String resource = "conf.xml";
        InputStream in = MybaitsTest.class.getClassLoader().getResourceAsStream(resource);
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
        /*自动提交事务*/
        SqlSession session = factory.openSession(true);
        String statement = "com.lzj.mybaits.test1.userMapper.getUser";
        User user = session.selectOne(statement, 3);
        System.out.println(user);
        session.close();
    }

}

运行测试类,输出结果如下

User [user_id=3, user_name=lzj, user_age=25]

二、通过指定字段映射

大多数情况下可以通过 标签指定表中字段和实体类中属性的映射。修改上面sql的userMapper.xml映射文件如下:




<mapper namespace="com.lzj.mybaits.test1.userMapper">

    
    <resultMap type="User" id="userResult">
        
        <id property="user_id" column="id"/>
        
        <result property="user_name" column="nane"/>
        <result property="user_age" column="age"/>
    resultMap>

    
    <select id="getUser" parameterType="int" resultMap="userResult">
        select id, name, age
        from users where id=#{id}
    select>

mapper>

你可能感兴趣的:(Mybaits)