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 ...
随机推荐
- 【Python矩阵及其基础操作】【numpy matrix】
一.矩阵生成 1.numpy.matrix: import numpy as np x = np.matrix([ [1, 2, 3],[4, 5, 6] ]) y = np.matrix( [1, ...
- svnserve: Can’t bind server socket: Address already in use报错解决办法
最近在学习自己搭建SVN服务,意外的报错 svnserve: Can’t bind server socket: Address already in use 于是google了下,原来是 已经启动了 ...
- kafka2x-Elasticsearch 数据同步工具demo
Bboss is a good elasticsearch Java rest client. It operates and accesses elasticsearch in a way simi ...
- ios中时间倒计时
博客地址 https://github.com/sundayios/SQCountTimeDown.git
- ScrollView不设置contentSize属性依然也可以作为底层滚动View(使用masonry设置scrollView的contentSize)
第一步 //下层的scroolView self.baseScrollView = [[UIScrollView alloc] init]; self.baseScrollView.delegate ...
- Servlet详细介绍
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" ...
- ZOJ4102 Array in the Pocket(2019浙江省赛)
贪心~ #include<bits/stdc++.h> using namespace std; ; int a[maxn]; int b[maxn]; int vis[maxn]; se ...
- PTA的Python练习题(八)
从 第3章-15 统计一行文本的单词个数 继续 1. s = input() count=0 for c in s: if c==' ': count=count+1 if c=='.': break ...
- KVM的客户机可以使用的存储
KVM的虚拟机可以直接使用宿主机器内的存储设备,比如可以把宿主机器内的硬盘直接暴露给 虚拟机挂载使用 -hda /dev/sfa(宿主机的设备文件) 还可以把镜像文件挂接到虚拟机,作为虚拟机的存储设备 ...
- jsp页面 将数据以Json 格式保存到数据库
1:jsp页面 <div class="control-group form-group all_activity"> <c:choose> <c:w ...