SpringBoot整合其他框架
SpringBoot整合Junit
实现步骤
- 搭建SpringBoot工程
- 引入starter-test起步依赖
- 编写测试类
- 添加测试相关注解
- @RunWith(SpringRunner.class)
- @SpringBootTest(classes=启动类.class)
- 编写测试方法
SpringBoot整合Redis(跳过)
实现步骤
- 搭建SpringBoot工程
- 引入redis起步依赖
- 配置redis相关属性
- 注入RedisTemplate模板
- 编写测试方法,测试
SpringBoot整合MyBatis
实现步骤
- 搭建SpringBoot工程
- 引入mybatis起步依赖,添加mysql驱动
- 编写DataSource和MyBatis相关配置
- 定义表和实体类
- 编写dao和mapper文件/纯注解开发
- 测试
com.itheima.springbootmybatis.domain.User
com.itheima.springbootmybatis.SpringbootMybatisApplication
注解版
XML配置版
application.yml
# datasource数据源配置
spring:
datasource:
url: jdbc:mysql:///ssm
username: root
password: 3306
driver-class-name: com.mysql.cj.jdbc.Driver
# mybatis配置
# mapper-location相当于XML中的<property name="mapperLocation>扫描Mapper层的配置文件
# type-aliases-package相当于XML中的<property name="typeAliasesPackage>别名配置,一般取其下实体类类名作为别名
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml #mapper的映射文件路径
type-aliases-package: com.itheima.springbootmybatis.domain
# config-location: #指定mybatis的核心配置文件
com.itheima.springbootmybatis.mapper.UserXmlMapper
package com.itheima.springbootmybatis.mapper;
import com.itheima.springbootmybatis.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface UserXmlMapper {
List<User> selectUserByIds(@Param("ids") List<Integer> ids);
int updateUserSet(User user);
List<User> selectUsersTrim(User user);
List<User> selectUsersChoose(User user);
/*
* 这个方法可以根据不同的条件来查询数据
* 如果有username,根据username去查
* 如果有password,根据password去查
* 如果两个都有,根据username和password去查询
* */
List<User> selectUsersIF(User user);
//根据username模糊查询
List<User> findAllUsersByUsername(String username);
//分页查询
List<User> findAllUsersByPage(Integer pageNum);
//根据id查询用户
User selectUserById(Integer id);
//根据id删除用户
Integer deleteUserById(Integer id);
//修改用户
int updateUser(User user);
//新增用户
int saveUser(User user);
//查询所有
List<User> findAllUsers();
}
sources/mapper/UserMapper.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.itheima.springbootmybatis.mapper.UserXmlMapper">
<insert id="saveUser">
insert into user (username,password) values (#{username},#{password})
</insert>
<update id="updateUser">
update user set username = #{username},password = #{password} where id = #{id}
</update>
<update id="updateUserSet" parameterType="com.itheima.springbootmybatis.domain.User">
update user
<set>
<if test="username != null and username !=''">
username = #{username},
</if>
<if test="password != null and password !=''">
password = #{password}
</if>
</set>
where id = #{id}
</update>
<delete id="deleteUserById">
delete from user where id = #{id}
</delete>
<!-- 写sql语句 -->
<!--
id:mapper接口中的方法名
resultType:方法的返回值类型,如果是entity类型,需要写全类名
parameterType:方法的入参的类型,可以省略
#{id}:代表方法的入参,类似于之前的?占位符,Mybatis底层使用的是什么?PreparedStatement
-->
<select id="selectUserById" resultType="com.itheima.springbootmybatis.domain.User">
select id,username,password from user where id = #{id}
</select>
<!-- 如果方法的返回值是集合,在映射xml中,resultType应该怎么写? -->
<select id="findAllUsers" resultType="com.itheima.springbootmybatis.domain.User">
select id,username,password from user
</select>
<select id="findAllUsersByPage" resultType="com.itheima.springbootmybatis.domain.User">
select id,username,password from user limit #{pageNum},8
</select>
<select id="findAllUsersByUsername" resultType="com.itheima.springbootmybatis.domain.User">
-- select id,username,password from user where username like '%${username}%'
select id,username,password from user where username like #{username}
</select>
<!-- sql片段 -->
<sql id="selectUser">
select id,username,password from user
</sql>
<!-- 多条件的查询,可以拼成多个条件,只要条件满足,都会看 -->
<select id="selectUsersIF" resultType="com.itheima.springbootmybatis.domain.User">
-- select id,username,password from user
-- where 1 = 1
-- 引用sql片段
<include refid="selectUser"></include>
<where>
<if test="id != null">
and id = #{id}
</if>
<if test="username != null and username !=''">
and username = #{username}
</if>
<if test="password != null and password !=''">
and password = #{password}
</if>
</where>
</select>
<!-- 类似于我们之前学过的switch...case,只会走一个条件,如果发现有满足的条件,后面的就不看了 -->
<select id="selectUsersChoose" resultType="com.itheima.springbootmybatis.domain.User">
select id,username,password from user
<where>
<choose>
<when test="username != null and username !=''">
username = #{username}
</when>
<when test="password != null and password !=''">
password = #{password}
</when>
<otherwise>
id = #{id}
</otherwise>
</choose>
</where>
</select>
<!-- trim去掉一些特殊的sql语法,比如我们常见的and,or,trim元素我们需要去掉的一些特殊的字符串,现在已经很少用了 -->
<select id="selectUsersTrim" resultType="com.itheima.springbootmybatis.domain.User">
select id,username,password from user
-- prefix:前缀 prefixOverrides:覆盖前缀
<trim prefix="where" prefixOverrides="and">
<if test="username != null and username != ''">
and username=#{username}
</if>
<if test="id != null">
and id = #{id}
</if>
</trim>
</select>
<select id="selectUserByIds" resultType="com.itheima.springbootmybatis.domain.User">
select id,username,password from user
where id in
-- collection:要遍历的集合
-- open:前置的括号(
-- close:后置的括号)
-- separator:每个数据遍历出来的分隔符
-- item:遍历出来的每一项
-- (1,5,6)
<foreach collection="ids" open="(" close=")" separator="," item="id">
#{id}
</foreach>
</select>
</mapper>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY