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}) 这里是实体的对应名字,
这两处对了就不需要配置其他的了。
mybatis 插入实体与数据库中的字段不一致的解决方案的更多相关文章
- mybatis插入实体到数据库后获取自增的主键
话不多说,直接说方法. 1.在insert语句中加入如下的代码. <insert id="insertSelective" parameterType="com.q ...
- Mybatis插入实体类字段为关键字解决方案
1. Mybatis插入实体类字段为关键字解决方案 1.1. 前言 可能你插入字段为关键字时报如下错误,且字段名不适合改变 You have an error in your SQL syntax; ...
- 解决SpringDataJpa实体类中属性顺序与数据库中生成字段顺序不一致的问题
一.在application.yml配置中添加数据库根据实体类自动创建数据库表的配置(这里数据库采用MySQL数据库) jpa: database: MYSQL show-sql: true #Hib ...
- 使用jpa时,实体类有不存在数据库中的字段
使用jpa时,实体类有不存在数据库中的字段.在改属性上面加上这个注解@Transient就可以解决问题.
- 关于从JSP页面插入数据到数据库中乱码问题的解决
问题描述:最近我在写一个j2ee的留言板系统模块,遇到了一个非常让我头大的问题,当我从JSP页面输入数据后,通过hibernate中的业务逻辑类HQL语句把这个数据插入到本地的mysql数据库中,可是 ...
- MySQL数据库中tinyint字段值为1,读取出来为true的问题
原文:https://blog.csdn.net/shuyou612/article/details/46788475 MySQL数据库中tinyint字段值为1,读取出来为true的问题 今天在 ...
- 当数据库中的字段与javabean中对应的属性名不同
当数据库中的字段与javabean中对应的属性名不同时: 在查询语句中对不同的字段起别名,例如: 数据库中的字段名为last_name , javabean中为lastName则:select las ...
- 镜像切换Logreader Agent报错:分发数据库中可能存在不一致的状态(续)
报错: 分发数据库中可能存在不一致的状态: dist_backup_lsn {00000030:000001ba:0004},dist_last_lsn {00000030:000001cd:0004 ...
- mybatis高级(2)_数据库中的列和实体类不匹配时的两种解决方法_模糊查询_智能标签
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "- ...
随机推荐
- 我为什么一直不愿意用bootstrap
做前端有2年多的时间了,知道bootstrap已经很久了. 第一次了解bootstrap是1年前,公司的一次培训中. 当时感到非常的愤怒,因为对框架的了解不够深入产生了这样的一个想法: 怎么会有这种框 ...
- Julia - 复合表达式
复合表达式是用一个表达式按照顺序对一系列子表达式求值,并返回最后一个子表达式的值 有两种方法:begin 块和 “;” 链 begin 块 begin 块的多行写法 julia> a = beg ...
- 【转】详解Data Binding 通过几个简单示例深入了解WinForm数据绑定特性
原文地址:http://www.cnblogs.com/lichence/archive/2012/02/17/2356001.html
- swoole学习
<?php ini_set('default_socket_timeout', -1); class serverEmail { public $serv = null; public func ...
- Nodejs&Express
http://www.expressjs.comhttp://github.com/lelandtseng/form-datahttp://github.com/lelandtseng/mongo-m ...
- leetcode260
public class Solution { public int[] SingleNumber(int[] nums) { var dic = new Dictionary<int, int ...
- leetcode575
public class Solution { public int DistributeCandies(int[] candies) { var dic = new Dictionary<in ...
- js里面的三种注释方法
javascript(js)语言里面的注释方法有三种. 第一种是多行注释"/**/",一般js文件开头,介绍作者,函数等信息. /* *author:xxx *day:2008-0 ...
- delphi 三层架构简单例子(经测试成功)
delphi 三层架构简单例子(经测试成功) 转载 2013年12月19日 09:48:57 1100 所谓三层: (1) 客户端 (2) 服务器端 (3) 数据库 在数据访问时,使得客户端必须通过服 ...
- cocoapods使用问题集锦(2017-04)
今天公司在公司新发的电脑上边安装cocoapod发现容易忘记的几个问题,感觉需要记录下来. 问题一:系统默认ruby镜像的卸载命令行 --> gem sources --remove h ...