四川建行网站,做网站公司职务,网站建设步骤,网站结构分析目录
1.MyBatis的基本操作
1.1增#xff08;Insert#xff09;
1.1.1返回主键 1.2删#xff08;Delete#xff09;
1.3改#xff08;Update#xff09;
1.4查#xff08;Select#xff09;
1.4.1起别名
1.4.2结果映射 1.4.3开启驼峰命名(推荐)
2.MyBatis XML配…目录
1.MyBatis的基本操作
1.1增Insert
1.1.1返回主键 1.2删Delete
1.3改Update
1.4查Select
1.4.1起别名
1.4.2结果映射 1.4.3开启驼峰命名(推荐)
2.MyBatis XML配置文件
2.1 配置连接字符串和MyBatis
2.2添加Mapper接口
2.3添加 UserInfoXMLMapper.xml
2.4增删改查操作
2.4.1增Insert 2.4.2删Delete
2.4.3改Update
2.4.4查Select 承接上文 详解MyBatis一
1.MyBatis的基本操作
1.1增Insert
SQL语句
insert into userinfo (username, password, age, gender, phone) values
(zhaoliu,zhaoliu,19,1,18700001234)
Mapper接口
把SQL中的常量替换为动态的参数直接使⽤UserInfo对象的属性名来获取参数 Insert(insert into userinfo (username, password, age, gender, phone) values (#{username}, #{password}, #{age}, #{gender}, #{phone}))Integer insert(UserInfo userInfo);
测试代码: Testvoid insert() {UserInfo userInfo new UserInfo();userInfo.setUsername(zhaoliu);userInfo.setPassword(zhaoliu);userInfo.setGender(2);userInfo.setAge(21);userInfo.setPhone(18612340005);userInfoMapper.insert(userInfo);}
运行后, 观察数据库执行结果 1.1.1返回主键
Insert 语句默认返回的是 受影响的行数
但有些情况下, 数据插入之后, 还需要有后续的关联操作, 需要获取到新插入数据的id 如订单系统、我们下完订单之后, 需要通知物流系统, 库存系统等, 这时候就需要拿到订单ID 如果想要拿到自增id, 需要在Mapper接口的方法上添加⼀个Options的注解 Options(useGeneratedKeys true, keyProperty id)Insert(insert into userinfo (username, age, gender, phone) values (#{userinfo.username}, #{userinfo.age}, #{userinfo.gender}, #{userinfo.phone}))Integer insert(UserInfo userInfo);useGeneratedKeys 是 MyBatis 中的一个属性设置。 当设置为 true 时它表示在执行插入操作后希望获取由数据库自动生成的主键值如果存在并将其设置到插入对象对应的属性中。这样可以方便地获取到新插入记录的主键值以便后续进行相关操作。 当在 MyBatis 的配置文件中设置了useGeneratedKeystrue时表示使用数据库的自增主键。而keyProperty属性则用于将自动生成的主键与实体类的属性进行绑定。通过将keyProperty设置为实体类中对应的属性名MyBatis 会在执行插入操作后将自动生成的主键值赋给该属性 例如如果实体类中有一个名为id的属性用于存储主键值可以将keyProperty设置为id。 测试代码 Testvoid insert() {UserInfo userInfo new UserInfo();userInfo.setUsername(Romised);userInfo.setPassword(Romised);userInfo.setGender(2);userInfo.setAge(22);userInfo.setPhone(1525523111);Integer count userInfoMapper.insert(userInfo);System.out.println(添加数据条数: count , 数据ID: userInfo.getId());}
运行结果 1.2删Delete
SQL 语句:
delete from userinfo where id 6;
Mapper接口
把SQL中的常量替换为动态的参数
Delete(delete from userinfo where id #{id})
void delete(Integer id);
1.3改Update
SQL 语句:
update userinfo set usernamezhaoliu where id5
Mapper接口
把SQL中的常量替换为动态的参数 Update(update userinfo set username#{username} where id#{id})void update(UserInfo userInfo);
1.4查Select
我们在上面查询时发现, 有几个字段是没有赋值的, 只有Java对象属性和数据库字段⼀模⼀样时, 才会进行赋值、接下来我们多查询⼀些数据 Select(select id, username, password, age, gender, phone, deleteFlag,
createTime, updateTime from userinfo)ListUserInfo queryAllUser()
查询结果 从运行结果上可以看到, 我们SQL语句中, 查询了delete_flag, create_time, update_time, 但是这几个属性却没有赋值. MyBatis 会根据方法的返回结果进⾏赋值. 方法⽤对象 UserInfo接收返回结果, MySQL 查询出来数据为⼀条, 就会自动赋值给对象. 方法⽤ListUserInfo接收返回结果, MySQL 查询出来数据为⼀条或多条时, 也会自动赋值给List. 但如果MySQL 查询返回多条, 但是方法使⽤UserInfo接收, MyBatis执⾏就会报错. 原因分析:
当自动映射查询结果时 MyBatis 会获取结果中返回的列名并在 Java 类中查找相同名字的属性忽略大小写 。 这意味着如果发现了 ID 列和 id 属性 MyBatis 会将列 ID 的值赋给 id 属性 解决方法
1.4.1起别名
在SQL语句中 给列名起别名 保持别名和实体类属性名⼀样
Select(select id, username, password, age, gender, phone, delete_flag as del, create_time as createTime, update_time as updateTime from userinfo)
public ListUserInfo queryAllUser(); SQL语句太长时, 使⽤加号进行字符串拼接 1.4.2结果映射
Select(select id, username, password, age, gender, phone, delete_flag, create_time, update_time from userinfo)
Results({Result(column delete_flag, property deleteFlag),Result(column create_time, property createTime),Result(column update_time, property updateTime)})ListUserInfo queryAllUser();
使用id属性给该Results定义别名使用ResultMap注解来服用其他定义的ResultMap 1.4.3开启驼峰命名(推荐) 通常数据库列使用蛇形命名法进⾏命名(下划线分割各个单词), 而Java 属性⼀般遵循驼峰命名法约定.为了在这两种命名方式之间启用自动映射 需要将mapUnderscoreToCamelCase设置为true。 mybatis:configuration:map-underscore-to-camel-case: true #配置驼峰⾃动转换 驼峰命名规则 abc_xyz abcXyz 表中字段名abc_xyz 类中属性名abcXyz Java代码中不做任何处理添加上述配置运行代码 字段全部进行正确赋值
2.MyBatis XML配置文件
使⽤Mybatis的注解方式 主要是来完成⼀些简单的增删改查功能. 如果需要实现复杂的SQL功能建议使用XML来配置映射语句也就是将SQL语句写在XML配置文件中.
2.1 配置连接字符串和MyBatis
此步骤需要进⾏两项设置 数据库连接字符串设置和 MyBatis 的 XML ⽂件配置。 如果是application.yml文件, 配置内容如下:
# 数据库连接配置
spring:datasource:url: jdbc:mysql://127.0.0.1:3306/mybatis_test?characterEncodingutf8useSSLusername: rootpassword: rootdriver-class-name: com.mysql.cj.jdbc.Driver
# 配置 mybatis xml 的⽂件路径在 resources/mapper 创建所有表的 xml ⽂件
mybatis:mapper-locations: classpath:mapper/**Mapper.xml
如果是application.properties文件配置内容如下
#驱动类名称
spring.datasource.driver-class-namecom.mysql.cj.jdbc.Driver
#数据库连接的url
spring.datasource.urljdbc:mysql://127.0.0.1:3306/mybatis_test?characterEncoding
#连接数据库的⽤户名
spring.datasource.usernameroot
#连接数据库的密码
spring.datasource.passwordroot
# 配置 mybatis xml 的⽂件路径在 resources/mapper 创建所有表的 xml ⽂件
mybatis.mapper-locationsclasspath:mapper/**Mapper.xml
2.2添加Mapper接口
Mapper
public interface UserInfoXMLMapper {ListUserInfo queryAllUser();Integer insert(UserInfo userInfo);Integer delete(Integer id);Integer update(Integer id,String username);
}2.3添加 UserInfoXMLMapper.xml
数据持久层的实现 MyBatis 的固定 xml 格式
?xml version1.0 encodingUTF-8?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.example.mybatistest.mapper.UserInfoXMLMapper/mapper 注意namespace的路径要和上面创建的Mapper路径一致按住Ctrl点击Mapper可以进入。 创建UserInfoXMLMapper.xml, 路径参考yml中的配置目录名和后缀要一致 2.4增删改查操作
2.4.1增Insert
UserInfoMapper接口
Integer insertUser(UserInfo userInfo);
UserInfoMapper.xml实现
其中设置useGeneratedKeys 和keyProperty属性可以返回自增id insert idinsert useGeneratedKeystrue keyPropertyidinsert into userinfo (username,password,age,gender,phone)values(#{username},#{password},#{age},#{gender},#{phone})/insert 如果使⽤Param设置参数名称的话, 使⽤方法和注解类似 2.4.2删Delete
UserInfoMapper接口
Integer deleteUser(Integer id);
UserInfoMapper.xml实现 delete iddeleteUserdelete from userinfo where id #{id}/delete
2.4.3改Update
UserInfoMapper接口
Integer updateUser(UserInfo userInfo);
UserInfoMapper.xml实现 update idupdateUserupdate userinfo set username#{username} where id#{id}/update
2.4.4查Select
同样的, 使⽤XML 的⽅式进⾏查询, 也存在数据封装的问题 我们把SQL语句进⾏简单修改, 查询更多的字段内容 select idqueryAllUser resultTypecom.example.demo.model.UserInfoselect id, username,password, age, gender, phone, delete_flag, create_time/select
运行结果: 结果显⽰: deleteFlag, createTime, updateTime 也没有进行赋值.
解决办法和注解类似:1. 起别名、2. 结果映射、3. 开启驼峰命名
其中1,3的解决办法和注解⼀样,不再多说, 接下来看下xml如果来写结果映射 Mapper.xml resultMap idBaseMap typecom.example.demo.model.UserInfoid columnid propertyid/idresult columndelete_flag propertydeleteFlag/resultresult columncreate_time propertycreateTime/resultresult columnupdate_time propertyupdateTime/result/resultMapselect idqueryAllUser resultMapBaseMapselect id, username,password, age, gender, phone, delete_flag, create_time/select