MyBatis-Insert、Delete、Update的注意事项
MyBatis-Insert、Delete、Update的注意事项
插入/更新乱码的解决
出现插入乱码,首先要考虑数据库的编码集是不是UTF-8
如果数据库的编码无误,查看MyBatis的全局配置文件中有没有配置编码集。
下面是正确的配置方式。
<?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>
<!--配置SQL打印-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings> <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://rayfoo.cn:3306/test?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="****"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="cn/rayfoo/mapper/CustomerMapper.xml"/>
</mappers>
</configuration>
插入后数据库不生效的解决
插入不生效考虑SqlSession是否提交。下面我们会展示正确的代码。此方法同样适用删除和更新。
package cn.rayfoo.bean; import lombok.Data;
import lombok.NoArgsConstructor; /**
* Created by rayfoo@qq.com Luna on 2020/2/4 15:29
*/
@Data@NoArgsConstructor
public class Customer {
private Integer cust_id;
private String cust_name;
private String cust_profession;
private String cust_phone;
private String email; public Customer(String cust_name, String cust_profession, String cust_phone, String email) {
this.cust_name = cust_name;
this.cust_profession = cust_profession;
this.cust_phone = cust_phone;
this.email = email;
}
}
package cn.rayfoo.util; 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;
import java.io.InputStream; /**
* Created by rayfoo@qq.com Luna on 2020/2/5 10:56
*/
public class MyBatisUtil { /**
* 获取一个Session
* @return
* @throws IOException
*/
public static SqlSession getSession() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-cfg.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
return sqlSessionFactory.openSession();
} }
package cn.rayfoo.mapper; import cn.rayfoo.bean.Customer; import java.util.List; /**
* Created by rayfoo@qq.com Luna on 2020/2/4 15:45
*/
public interface CustomerMapper { /**
* 查询所有记录
*
* @return
*/
List<Customer> findAll(); Customer findOne(Integer cust_id); int insertCustomer(Customer customer); }
<?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="cn.rayfoo.mapper.CustomerMapper">
<insert id="insertCustomer" parameterType="cn.rayfoo.bean.Customer">
insert into customer(cust_id,cust_name,cust_profession,cust_phone,email)
values(#{cust_id},#{cust_name},#{cust_profession},#{cust_phone},#{email});
</insert>
<select id="findAll" resultType="cn.rayfoo.bean.Customer">
select * from customer
</select>
<select id="findOne" resultType="cn.rayfoo.bean.Customer">
select * from customer where cust_id = #{cust_id}
</select>
</mapper>
package cn.rayfoo.test; import cn.rayfoo.bean.Customer;
import cn.rayfoo.mapper.CustomerMapper;
import cn.rayfoo.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.io.IOException; /**
* Created by rayfoo@qq.com Luna on 2020/2/4 15:58
*/
public class TestApp { @Test
public void test01() throws IOException {
SqlSession session = MyBatisUtil.getSession();
CustomerMapper mapper = session.getMapper(CustomerMapper.class);
Customer all = mapper.findOne(1);
System.out.println(all);
session.close();
} @Test
public void test02() throws IOException {
SqlSession session = MyBatisUtil.getSession();
Customer customer = new Customer( "张三", "码农", "10086", "10086@qq.com");
int insert = session.insert("insertCustomer",customer);
//此处需要进行提交
session.commit();
session.close();
} @Test
public void test03() throws IOException {
SqlSession session = MyBatisUtil.getSession();
CustomerMapper mapper = session.getMapper(CustomerMapper.class);
Customer customer = new Customer( "张三", "码农", "10086", "10086@qq.com");
mapper.insertCustomer(customer);
//此处需要进行提交
session.commit();
session.close();
} }
插入POJO对象的注意事项
为了防止SQL注入,我们尽量使用#{},在插入POJO对象的属性时,#{}中使用POJO对象的属性名!!!
参考上述代码。
MyBatis-Insert、Delete、Update的注意事项的更多相关文章
- MyBatis insert/delete/update 的返回值
insert,返回值是:新插入行的主键(primary key):需要包含<selectKey>语句,才会返回主键,否则返回值为null. <insert id="inse ...
- 带有OUTPUT的INSERT,DELETE,UPDATE
原文地址:http://blog.sina.com.cn/s/blog_71460d950100nld2.html OUTPUT是SQL SERVER2005的新特性.可以从数据修改语句中返回输出.可 ...
- Use Select To Generate Any Insert/Delete/Update Statement
If you don't have the permission to generate script according to an existing db, but you have the re ...
- sqlserver触发器insert,delete,update
Create Trigger [dbo].[upemployee_kefyu_sale] on [dbo].[employee] for update as if update(FullName) b ...
- sql server 带有OUTPUT的INSERT,DELETE,UPDATE
原文:sql server 带有OUTPUT的INSERT,DELETE,UPDATE OUTPUT是SQL SERVER2005的新特性.可以从数据修改语句中返回输出.可以看作是"返回结果 ...
- MySQL进阶10--DML数据操纵预言: insert/delete/update --多表连接修改/.多表连接删除/多表连接查询-- truncate 和 delete的区别
/* DML -- 数据操纵预言: insert/delete/update */ #一: 插入语句 /* 语法1: insert into 表名(列名,..,列名....) values(值1,值2 ...
- mybatis insert、update 、delete默认返回值解释与如何设置返回表主键
在使用mybatis做持久层时,insert.update.delete,sql语句默认是不返回被操作记录主键的,而是返回被操作记录条数: 那么如果想要得到被操作记录的主键,可以通过下面的配置方式获取 ...
- mybatis insert转update,duplicate关键字的使用示例,及返回情况说明
主键存在时又insert转为update某个关键字段,示例如下,注意,如果这条数据曾经不存在,此时执行insert返回条目是1,如果已存在,执行update返回条目是2!!!<insert id ...
- MyBatis日记(四):MyBatis——insert、update、delete、select
MyBatis简单增删改查操作,此处所做操作,皆是在之前创建的MyBatis的Hello world的工程基础上所做操作. 首先在接口文件(personMapper.java)中,添加操作方法: pa ...
- Mybatis Insert、update、delete流程
上文mybatis源码简书我们讲到sqlsession中通过executor来执行sql,我们接着往下看 update方法点进去,我们进到baseexecutor 这里我们看到 clearLocalC ...
随机推荐
- 2016-2017学年第三次测试赛 习题E 林喵喵算术
时间限制: 1 Sec 内存限制: 128 MB 提交: 70 解决: 25 提交统计讨论版 题目描述 给你两个八进制数,你需要在八进制计数法的情况下计算a-b. 如果结果为负数,你应该使用负号代 ...
- Python读取MNIST数据集
MNIST数据集获取 MNIST数据集是入门机器学习/模式识别的最经典数据集之一.最早于1998年Yan Lecun在论文: Gradient-based learning applied to do ...
- Android Studio如何更新support repository
转自: http://blog.csdn.net/sinat_29696083/article/details/70256377 刚进新公司,熟悉新业务中.老大叫我看看关于ConstraintLayo ...
- video标签 在微信浏览器打开,不弹出大的独立窗口 而是直接播放。
1.在 video 标签中添加 属性 x5-playsinline playsinline webkit-playsinline="true" 2.ckplayer的 ...
- 「CQOI2016」不同的最小割
「CQOI2016」不同的最小割 传送门 建出最小割树,把每一个点对的最小割抠出来 \(\text{unique}\) 一下就好了. 参考代码: #include <algorithm> ...
- unity 热更方案对比
现在一般使用的方案有:tulua&ulua.xlua.ILRuntime 对比: tulua&ulua 方案成熟,稳定第三方库支持 xlua 之前是为了热更修复线上bug的,腾讯发起的 ...
- select模块
select模块 1. select模块 源:select.py This module provides access to the select() and poll() functio ...
- python基础之省份三级菜单
菜单 menu = { #定义一个字典 '北京':{ '海淀':{ '五道口':{ 'soho':{}, '网易':{}, 'google':{} }, '中关村':{ '爱奇艺':{}, '汽车之家 ...
- Eclipse上将maven项目部署到tomcat,本地tomcat下,webapps下,web-inf下lib下没有jar包决绝方案
右键项目选择properties ,选择Deployment Assembly , 右边点击Add 然后选择 Java Build Path Entries ,,,next , 选择Mave ...
- 【PAT甲级】1006 Sign In and Sign Out (25 分)
题意: 给出学生人数M,输入M组学生ID,到机房的时间,离开机房的时间.输出最早到机房的学生的ID,空格,最后离开机房的学生的ID.(M大小未给出,就用了1e5) AAAAAccepted code: ...