一共有两个办法。1,修改sql语句。2,使用<resultMap>映射类属性名和字段的关系。xml方式和annotation注解都是相同的。
全部代码下载地址:https://git.oschina.net/zhuhongliang/SimpleMybatisDemo/tree/differentName
这个demo还是在前几篇的基础上修改来的,下面放代码:
先看下mysql数据库的字段名和实体类的属性名:
User.java
package com.zhl.obj; public class User { private int id_a; private String namea; private int newAgehehe; public int getId_a() { return id_a; } public void setId_a(int id_a) { this.id_a = id_a; } public String getNamea() { return namea; } public void setNamea(String namea) { this.namea = namea; } public int getNewAgehehe() { return newAgehehe; } public void setNewAgehehe(int newAgehehe) { this.newAgehehe = newAgehehe; } public String toString(){ return "User [id= "+id_a+",name= "+namea+",age= "+newAgehehe+"]"; } }
下面先看下xml文件的修改:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.zhl.mapping.userMapper"> <!-- 没有修改sql语句,resultMap的值是下面resultMap标签的id值 --> <!-- 查一个 --> <select id="getUser" parameterType="int" resultMap="getUserMap"> select * from users where id = #{id_a} </select> <!--这里用resultMap标签 映射user类和数据库字段 --> <resultMap type="User" id="getUserMap"> <!-- 用id属性来映射主键字段 --> <id property="id_a" column="id"/> <!-- 用result属性来映射非主键字段 --> <result property="newAgehehe" column="age"/> <result property="namea" column="name"/> </resultMap> <!-- 下面的都没什么修改,sql语句的变量要写实体类的属性:#{namea}, #{newAgehehe} --> <!-- 增 --> <insert id="addUser" parameterType="User"> insert into users(name, age) values(#{namea}, #{newAgehehe}) </insert> <!-- 删 --> <delete id="deleteUser" parameterType="String"> delete from users where name =#{namea} </delete> <!-- 改 --> <update id="updateUser" parameterType="User"> update users set name=#{namea}, age =#{newAgehehe} where id=#{id_a} </update> <!-- 直接修改sql语句 --> <!-- 查全部 --> <select id="getAll" resultType="User"> select id id_a, name namea, age newAgehehe from users </select> </mapper>
下面看看annotation版本的代码:
package com.zhl.mapping; import java.util.List; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Result; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; import org.apache.ibatis.annotations.Results; import com.zhl.obj.User; public interface UserMapperI { @Insert("insert into users (name, age) values(#{namea}, #{newAgehehe})") public int addUser(User user); //直接修改sql语句 @Select("select id id_a, name namea, age newAgehehe from users where id = #{id_a}") public User getUser(int id); //使用Result映射实体类的属性和数据库的字段 @Select("select * from users") @Results({ @Result(id=true,column="id",property="id_a"), @Result(column="name",property="namea"), @Result(column="age",property="newAgehehe") }) public List getAll(); @Update("update users set name= #{namea}, age=#{newAgehehe} where id= #{id_a}") public int updateUser (User user); @Delete("delete from users where name = #{namea}") public int deleteUser(String name); }
就这么简单,全部代码下载地址:https://git.oschina.net/zhuhongliang/SimpleMybatisDemo/tree/differentName