mybatis 插入实体与数据库中的字段不一致的解决方案

时间:2023-03-10 03:29:38
mybatis 插入实体与数据库中的字段不一致的解决方案

1、建立一个实体类

public class Student {

    private Integer id;
private String name;
private Double salary; public Student() {
} public Student(Integer id, String name, Double salary) {
this.id = id;
this.name = name;
this.salary = salary;
} 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 Double getSalary() {
return salary;
} public void setSalary(Double salary) {
this.salary = salary;
}
}

  2、建立数据库

CREATE  table student(
student_id int(5) PRIMARY KEY ,
student_name VARCHAR (10),
student_salary DOUBLE (8,2)
)

  4、配置文档

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 加载类路径下的属性文件 -->
<properties resource="db.properties"/> <!-- 设置类型别名 -->
<typeAliases>
<typeAlias type="com.liuyang.mybatis.student.bean.Students" alias="student"/>
</typeAliases> <!-- 设置一个默认的连接环境信息 -->
<environments default="mysql_developer"> <!-- 连接环境信息,取一个任意唯一的名字 -->
<environment id="mysql_developer">
<!-- mybatis使用jdbc事务管理方式 -->
<transactionManager type="jdbc"/>
<!-- mybatis使用连接池方式来获取连接 -->
<dataSource type="pooled">
<!-- 配置与数据库交互的4个必要属性 -->
<property name="driver" value="${mysql.driver}"/>
<property name="url" value="${mysql.url}"/>
<property name="username" value="${mysql.username}"/>
<property name="password" value="${mysql.password}"/>
</dataSource>
</environment> <!-- 连接环境信息,取一个任意唯一的名字 -->
<environment id="oracle_developer">
<!-- mybatis使用jdbc事务管理方式 -->
<transactionManager type="jdbc"/>
<!-- mybatis使用连接池方式来获取连接 -->
<dataSource type="pooled">
<!-- 配置与数据库交互的4个必要属性 -->
<property name="driver" value="${oracle.driver}"/>
<property name="url" value="${oracle.url}"/>
<property name="username" value="${oracle.username}"/>
<property name="password" value="${oracle.password}"/>
</dataSource>
</environment>
</environments> <!-- 加载映射文件-->
<mappers>
<!--demo_4-->
<mapper resource="com/liuyang/demo_4/StudentMapper2.xml"/> 这里是重点,加入你的个人的xml文档的路径样式如图
</mappers>
</configuration>

5、配置文档

<?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.liuyang.demo_4.Student">
<resultMap type="com.liuyang.demo_4.Student" id="studentMap">
<id property="id" column="student_id"/>
<result property="name" column="student_name"/>
<result property="salary" column="student_salary"/>
</resultMap> <!--这里不用parameterMap-->
<insert id="add" parameterType="com.liuyang.demo_4.Student">
insert into student(student_id,student_name,student_salary) values(#{id},#{name},#{salary}) 这里是重点
</insert> <!--查询一个学生-->
<select id="findById" parameterType="int" resultMap="studentMap">
SELECT student_id,student_name,student_salary from student WHERE student_id =#{student_id}
</select>
</mapper>

红字部分,其实

<insert id="add"  parameterType="com.liuyang.demo_4.Student">
insert into student(student_id,student_name,student_salary) values(#{id},#{name},#{salary}) 这里是重点
</insert>
这段代码与上边的
<resultMap type="com.liuyang.demo_4.Student" id="studentMap">
<id property="id" column="student_id"/>
<result property="name" column="student_name"/>
<result property="salary" column="student_salary"/>
</resultMap>
这里不反冲,主要是写好sql语句,其他都是正常配置
insert into student(student_id,student_name,student_salary) values(#{id},#{name},#{salary}) 
前边student
(student_id,student_name,student_salary) 这里是表中的字段名
后边
values(#{id},#{name},#{salary})      这里是实体的对应名字,
这两处对了就不需要配置其他的了。