目录
MyBatis XML配置文件
MyBatis的开发有两种方式:注解;XML。
上一篇文章详细讲解了注解的方式,接下来讲解XML的方式。
使用MyBatis的注解方式主要是来完成一些简单的增删改查功能,如果需要实现复杂的SQL功能,建议使用XML来配置映射语句,也就是将SQL语句写在XML配置文件中。
MyBatis XML的方式需要以下两步:
1.配置数据库连接字符串和MyBatis
2.写持久层代码
配置数据库连接字符串和MyBatis
此步骤需要进⾏两项设置,数据库连接字符串设置和 MyBatis 的 XML ⽂件配置。
如果是application.yml⽂件, 配置内容如下:
# 数据库连接配置 spring: datasource: url: jdbc:mysql://127.0.0.1:3306/mybatis_test?characterEncoding=utf8&useSSL=false username: root password: hxwhly2. driver-class-name: com.mysql.cj.jdbc.Driver mybatis: # 配置 mybatis xml 的⽂件路径,在 resources/mapper 创建所有表的 xml ⽂件 mapper-locations: classpath:mybatis/**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
写持久层代码
持久层代码分为两部分:
1.方法定义 interface
2.方法实现 XXX.xml
添加mapper接口
@Mapper
public interface UserInfoXmlMapper {
Integer insert(UserInfo userInfo);
}
添加xml格式文件
<?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.wmh.mybatisdemo.mapper.UserInfoXmlMapper">
<select id="selectAllUser" resultType="com.wmh.mybatisdemo.model.UserInfo">
select * from userinfo
</select>
</mapper>
<mapper>标签:需要指定namespace属性,表示命名空间,值为mapper接口的全限定名。
<select>查询标签:使用率执行数据库的查询操作的
<id>:是和 interface接口中定义的方法名称是一样的,表示对接口的具体方法实现
<resultType>:是返回的数据类型。
在resources路径下创建xml格式文件
注意:
1.配置文件中的classpath指的是resources;
2.配置文件中的mybatis指的是resources目录下的mybatis目录;
3.配置文件中的**Mapper.xml相当于resources的mybatis下的所有以Mapper.xml格式结尾的文件。
增删改查操作
增加(Insert)
mapper接口
@Mapper
public interface UserInfoXmlMapper {
Integer insert(UserInfo userInfo);
}
xml实现
<insert id="insert">
insert into userinfo (username, password, age, gender)
values (#{username}, #{password}, #{age}, #{gender})
</insert>
Test代码
@SpringBootTest
class UserInfoXmlMapperTest {
@Autowired
private UserInfoXmlMapper userInfoXmlMapper;
@Test
void insert() {
UserInfo userInfo = new UserInfo();
userInfo.setUsername("zhaoliu");
userInfo.setPassword("zhaoliu");
userInfo.setAge(12);
userInfo.setGender(1);
userInfoXmlMapper.insert(userInfo);
}
}
运行结果:
参数重命名
如果使⽤@Param设置参数名称的话, 使⽤⽅法和注解类似
UserInfoMapper接⼝:
Integer insert(@Param("userinfo") UserInfo userInfo);
<insert id="insert">
insert into userinfo (username, password, age, gender)
values (#{userinfo.username}, #{userinfo.password}, #{userinfo.age}, #{userinfo.gender})
</insert>
返回自增id
mapper接口不变,只需xml文件增加useGeneratedKeys和keyProperty即可。
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
insert into userinfo (username, password, age, gender)
values (#{username}, #{password}, #{age}, #{gender})
</insert>
删除(Delete)
mapper接口
@Mapper
public interface UserInfoXmlMapper {
Integer delete(Integer id);
}
xml实现
<delete id="delete">
delete from userinfo where id = #{id}
</delete>
修改(Update)
mapper接口
@Mapper
public interface UserInfoXmlMapper {
Integer update(UserInfo userInfo);
}
xml实现
<update id="update">
update userinfo set password = #{password} where id =#{id}
</update>
查询(Select)
mapper接口
@Mapper
public interface UserInfoXmlMapper {
List<UserInfo> selectAllUser();
}
xml实现
<select id="selectAllUser" resultType="com.wmh.mybatisdemo.model.UserInfo">
select * from userinfo
</select>
Test代码
运行结果:
我们注意到,delete_flag, create_time, update_time这几个属性是没有被赋值的,但是其它项都有被赋值,为什么呢?我们看一下数据库表结构和UserInfo属性:
此时,我们不难想到,大概是因为数据库表结构和UserInfo属性名称不一致的原因,当数据库表结构和UserInfo属性名称一致时,才能被成功赋值;否则,就会赋值失败。
属性名称不同的解决方法
1.起别名
2.结果映射
3.开启驼峰命名
其中1和3的解决方法和注解一样,下面讲解xml如何进行结果映射。
<resultMap id="BaseMap" type="com.wmh.mybatisdemo.model.UserInfo">
<id column="id" property="id"></id>
<result column="delete_flag" property="deleteFlag"></result>
<result column="create_time" property="createTime"></result>
<result column="update_time" property="updateTime"></result>
</resultMap>
解释:
多表查询
ArticleInfo
@Data
public class ArticleInfo {
private Integer id;
private String title;
private String content;
private Integer uid;
private Integer deleteFlag;;
private Date createTime;
private Date updateTime;
}
mapper接口
@Mapper
public interface ArticleInfoMapper {
ArticleInfo selectArticleAndUserById(Integer id);
}
接口实现
<?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.wmh.mybatisdemo.mapper.ArticleInfoMapper">
<select id="selectArticleAndUserById" resultType="com.wmh.mybatisdemo.model.ArticleInfo">
select ta.*, tb.username, tb.gender from articleinfo ta
left join userinfo tb
on ta.uid = tb.id
where ta.id = #{id}
</select>
</mapper>
数据库中userinfo表和 articleinfo表信息:
articleinfo表:
userinfo表:
运行结果: