MyBatis-InsertDeleteUpdate的注意事项

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyBatis-InsertDeleteUpdate的注意事项相关的知识,希望对你有一定的参考价值。

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&amp;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-InsertDeleteUpdate的注意事项的主要内容,如果未能解决你的问题,请参考以下文章