文章目录

配置数据库连接和MyBatis写持久层代码添加mapper接口添加UserInfoXMLMapper.xml单元测试

CRUD增(Insert)删(Delete)改(Update)查(Select)

MyBatis XML的⽅式需要以下两步:

配置数据库连接字符串和MyBatis 写持久层代码

配置数据库连接和MyBatis

application.yml⽂件配置内容如下:

spring:

datasource:

url: jdbc:mysql://127.0.0.1:3306/mybatis_test?

characterEncoding=utf8&useSSL=false

username: root

password: 你自己设置的密码

driver-class-name: com.mysql.cj.jdbc.Driver

mybatis:

configuration: # 配置打印 MyBatis⽇志

log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

mapper-locations: classpath:mapper/**Mapper.xml

如果是application.properties⽂件,配置内容如下:

#驱动类名称

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

#数据库连接的url

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mybatis_test?

characterEncoding=utf8&useSSL=false

#连接数据库的⽤⼾名

spring.datasource.username=root

#连接数据库的密码

spring.datasource.password=root

# 配置 mybatis xml 的⽂件路径,在 resources/mapper 创建所有表的 xml ⽂件

mybatis.mapper-locations=classpath:mapper/**Mapper.xml

写持久层代码

持久层代码分两部分

⽅法定义:Interface⽅法实现:XXX.xml

添加mapper接口

数据持久层的接⼝定义:

@Mapper

public interface UserInfoXMlMapper {

}

添加UserInfoXMLMapper.xml

数据持久成的实现,MyBatis的固定xml格式:

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

创建UserInfoXMLMapper.xml,路径参考yml中的配置 查询所有⽤⼾的具体实现:

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

以下是对以上标签的说明: • 标签:需要指定namespace 属性,表⽰命名空间,值为mapper接⼝的全限定 名,包括全包名.类名。 • 查询标签:是⽤来执⾏数据库的查询操作的: ◦ id :是和Interface (接⼝)中定义的⽅法名称⼀样的,表⽰对接⼝的具体实现⽅法。 ◦ resultType :是返回的数据类型,也就是开头我们定义的实体类.

单元测试

@SpringBootTest

class UserInfoMapperTest {

@Autowired

private UserInfoMapper userInfoMapper;

@Test

void queryAllUser() {

List userInfoList = userInfoMapper.queryAllUser();

System.out.println(userInfoList);

}

}

CRUD

增(Insert)

UserInfoMapper接⼝:

Integer insertUser(UserInfo userInfo);

UserInfoMapper.xml实现:

insert into userinfo (username, `password`, age, gender, phone) values (#

{username}, #{password}, #{age},#{gender},#{phone})

如果使⽤@Param设置参数名称的话,使⽤⽅法和注解类似 UserInfoMapper接⼝:

Integer insertUser(@Param("userinfo") UserInfo userInfo);

UserInfoMapper.xml实现:

insert into userinfo (username, `password`, age, gender, phone) values

(#{userinfo.username},#{userinfo.password},#{userinfo.age},#

{userinfo.gender},#{userinfo.phone})

返回⾃增id 接⼝定义不变,Mapper.xml实现设置useGeneratedKeys和keyProperty属性

insert into userinfo (username, `password`, age, gender, phone) values

(#{userinfo.username},#{userinfo.password},#{userinfo.age},#

{userinfo.gender},#{userinfo.phone})

删(Delete)

UserInfoMapper接⼝:

Integer deleteUser(Integer id);

UserInfoMapper.xml实现:

delete from userinfo where id = #{id}

改(Update)

UserInfoMapper接⼝:

Integer updateUser(UserInfo userInfo);

UserInfoMapper.xml实现:

update userinfo set username=#{username} where id=#{id}

查(Select)

同样的,使⽤XML的⽅式进⾏查询,也存在数据封装的问题 我们把SQL语句进⾏简单修改,查询更多的字段内容

结果显⽰:deleteFlag,createTime,updateTime也没有进⾏赋值. 解决办法和注解类似:

起别名结果映射开启驼峰命名 其中1,3的解决办法和注解⼀样,不再多说,接下来看下xml如果来写结果映射

Mapper.xml

⭕总结 感谢大家的阅读,希望得到大家的批评指正,和大家一起进步,与君共勉!

推荐链接

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。