Mybatis的组成

导入包

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>

Mybatis的组成

1.数据对象的映射类
2.Mybatis配置
3.对应的接口类
4.Mapper的xml文件

1、数据对象

@Data
public class UserDo {
   private String userName;
   private String password;
   private Integer id;
}

2、配置mybatis,

配置数据库连接

1)useUnicode是否使用Unicode字符集, 如果参数characterEncoding设置为gb2312或gbk, 本参数值必须设置为true
2)characterEncoding 当useUnicode设置为true, 给定编码, 常用utf8,
3)autoReconnect 当数据库连接异常中断时, 是否自动重新连接?,默认是: true
4)allowMultiQueries 允许一个statement执行多个用;分割的sql, 默认false,可以改为true

配置mybaitis

1)扫描xml的路径
2)扫描映射类的路径,多个包逗号隔开
3)配置sql日志输出

#配置数据库连接
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/robot?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=******
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
srping.datasource.druid.initial-size=2

#配置mybatis
mybatis.mapper-locations=classpath:/mappers/*Mapper.xml
mybatis.type-aliases-package=com.robot.model
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

3、在接口类上面加上@mapper,就会被mybatis识别,

@Mapper
public interface UserMapper {
    public int addUser(UserDo userDo);

    public List<UserDo> selectUser(UserDo userDo);

    public UserDo selectUserById(Integer id);
}

4、 Mybatis的sql全部写在xml文件里面,那么真正执行的时候是调用mybatis的接口然后执行xml里面的sql,那接口类和xml文件是如何关联的呢?
看xml里面的namespace等于接口类的全称,这样他们就关联上行了
例如:
1)namespace="com.robot.mapper.UserMapper"

2)d要等于接口类里的方法名,例如addUser,

3)新增的时候使用例如,paramterType="com.robot.model.UserDao"

4)查询的时候使用resutlType="com.robot.model.UserDao"

<?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.robot.mapper.UserMapper">
   <insert id="addUser" parameterType="com.robot.model.UserDo"
      useGeneratedKeys="true" keyColumn="id" keyProperty="id">
      INSERT INTO user (user_name,password,create_time,update_time)
      VALUES (#{userName},#{password},now(),now())
   </insert>

   <select id="selectUser" resultType="com.robot.model.UserDo">
      select * from user
   </select>

   <select id="selectUserById" resultType="com.robot.model.UserDo">
      select * from user
      WHERE id=#{0}
   </select>
</mapper>   

一、新增

下面3个是插入后返回主键使用,非必须
 useGeneratedKeys="true" keyColumn="id" keyProperty="id">,

<insert id="addUser" parameterType="com.robot.model.UserDo"
      useGeneratedKeys="true" keyColumn="id" keyProperty="id">
      INSERT INTO user (user_name,password,create_time,update_time)
      VALUES (#{userName},#{password},now(),now())
   </insert>

切记
接口和xml文件一定要名字一样,否则绑定不上
例如:

UserMapper
UserMapper.xml xml配置文件

去重
修改数据库字段,user_name 为unique
例子:
navicat 插件里面有 不能重复,用设置索引,建立了一个数据表,其中一个字段不是主键,但在存数据的时候又不想让它有重复的值,例如user_name 

Java代码异常:

DuplicateKeyException
捕获异常确认,新增唯一性

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NeilNiu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值