Mybatis和spingboot整合
0. 导包
<!-- 统一管理springboot相关的包 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <dependencies>
<!-- controller和前端交互; springboot和前端整合 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springboot和mybatis整合 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<!-- mysql驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- 红辣椒 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
1.创建 application.properties 文件
#数据库配置
spring.datasource.password=ladeng
spring.datasource.url=jdbc:mysql://localhost:3306/schoolSystem??serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver #时间格式化
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
#输出日志的级别
logging.level.com.xmg.mapper=debug
2. 创建一个启动类
需要贴人的标签
@MapperScan("需要扫描的包名")
@SpringBootApplication 这个是启动标签
3. Controller类
@Controller // 可以在一个类中定义多个接口
@RequestMapping("/stuController")
public class StuController {
@Autowired
private StuService stuService; @RequestMapping("/list")
@ResponseBody // json格式
public List<Stu> list() {
return stuService.list();
}
}
4. Mapper.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<mapper namespace="Mapper接口全限定名"> <resultMap type="domain全限定名" id="stuMap">
<!--
column:数据库例
property:可以给column里面写的参数取别名
javaType: java的类型
jdbcType:数据库的类型
-->
<id column="id" property="id" javaType="java.lang.Long" jdbcType="BIGINT"/>
<result column="name" property="name" javaType="java.lang.String" jdbcType="VARCHAR"/>
<result column="age" property="age" javaType="java.lang.Integer" jdbcType="INTEGER"/>
<result column="course_id" property="courseId" javaType="java.lang.Long" jdbcType="BIGINT"/>
<result column="create_time" property="createTime" javaType="java.sql.Date" jdbcType="DATE"/>
<result column="modify_time" property="modifyTime" javaType="java.sql.Date" jdbcType="DATE"/>
</resultMap> <sql id="filedId" >id, name, age, course_id, create_time</sql> <insert id="addStu" parameterType="com.xmg.domain.Stu" >
insert into stu (name, age, course_id, create_time)
values (#{name}, #{age}, #{courseId}, #{createTime})
</insert> <select id="getStu" resultMap="stuMap" >
select
<include refid="filedId" />
from stu
<!--
prefix: 在前面添加where
suffixOverrides:如果后面没有条件就去除and
-->
<trim prefix="where" suffixOverrides="and" >
<if test="id != null">
id = #{id} and
</if>
<if test="name2 != null and name2 != ''" >
name = #{name2} and
</if>
</trim>
limit 1
</select> <select id="selectByNameList" resultType="com.xmg.domain.Stu" >
select
<include refid="filedId" />
from stu
<trim prefix="where name in ">
<!--
open: 最开始拼接 '('
close: 结束后拼接 ')'
separator: 用什么分割
-->
<foreach collection="nameList" item="name" open="(" close=")" separator=",">
#{name}
</foreach>
</trim>
</select>
</mapper>
Mybatis和spingboot整合的更多相关文章
- spingBoot整合mybatis+generator+pageHelper
spingBoot整合mybatis+generator+pageHelper 环境/版本一览: 开发工具:Intellij IDEA 2018.1.4 springboot: 2.0.4.RELEA ...
- 【SpringBoot】SpingBoot整合AOP
https://blog.csdn.net/lmb55/article/details/82470388 [SpringBoot]SpingBoot整合AOPhttps://blog.csdn.net ...
- spring+mybatis+oracle/mysql整合开发需要的jar包详解
导入spring,mybatis,c3p0,oracle和mybatis提供的与spring整合的插件包 mysql的jar: mysql-connector-java-5.1.7 ...
- MyBatis学习(四)MyBatis和Spring整合
MyBatis和Spring整合 思路 1.让spring管理SqlSessionFactory 2.让spring管理mapper对象和dao. 使用spring和mybatis整合开发mapper ...
- Mybatis与Spring整合,使用了maven管理项目,作为初学者觉得不错,转载下来
转载自:http://www.cnblogs.com/xdp-gacl/p/4271627.html 一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: mvn archetype ...
- spring与mybatis三种整合方法
spring与mybatis三种整合方法 本文主要介绍Spring与Mybatis三种常用整合方法,需要的整合架包是mybatis-spring.jar,可通过链接 http://code.googl ...
- Mybatis+struts2+spring整合
把student项目改造成ssm struts2 +mybatis+spring 1,先添加spring支持:类库三个,applicationContext.xml写在webinf下四个命名空间,监 ...
- mybatis与spring整合(基于配置文件)
本文主要介绍了如何将mybatis和spring整合在一起使用,本人使用的是mybatis3.05 + spring3.1.0M2 ,使用dbcp作为数据库连接池. 1.编写数据访问接口(UserDa ...
- mybatis与spring整合时读取properties问题的解决
在学习mybatis与spring整合是,想从外部引用一个db.properties数据库配置文件,在配置文件中使用占位符进行引用,如下: <context:property-placehold ...
随机推荐
- JS函数 编程练习 使用javascript代码写出一个函数:实现传入两个整数后弹出较大的整数。
编程练习 使用javascript代码写出一个函数:实现传入两个整数后弹出较大的整数. 任务 第一步: 编写代码完成一个函数的定义吧. 第二步: 我们来补充函数体中的控制语句,完成函数功能吧. 提示: ...
- The linux command 之权限
一.修改权限 只有文件主或者超级用户才可以修改文件或者目录的权限. 符号表示法分为三种: Who the change will affect Which operation will be perf ...
- Windows shutdown
用法: shutdown [/i | /l | /s | /sg | /r | /g | /a | /p | /h | /e | /o] [/hybrid] [/soft] [/fw] [/f] ...
- PL/SQL创建用户
步骤一:新建 步骤二:填写信息 对应SQL代码 -- Create the user create user WENT identified by "longrise" defau ...
- day32--面向对象的程序设计之继承实现的原理(继承顺序)、封装、property
Python之路,Day19 = 面向对象的程序设计之继承实现的原理(继承顺序).封装.property 以下类容参考自:http://www.cnblogs.com/metianzing/p/712 ...
- day14 python02---字符串
day02 数字相关的转换 bin() 2进制oct() 8进制hex() 16进制 字符串 定义:它是一个有序的字符的集合,用于存储和表示基本的文本信息,‘’或“”或‘’‘ ’‘’中间包含的内容称之 ...
- Java集合综述
Java集合图,虚线框为接口,实线框是具体的类 具体实现类 基本使用 (1)List: List基本操作 ArrayList<String> arrayList = new ArrayLi ...
- k8s 安装
1.1 k8s的架构 除了核心组件,还有一些推荐的Add-ons: 组件名称 说明 kube-dns 负责为整个集群提供DNS服务 Ingress Controller 为服务提供外网入口 Heaps ...
- 《DSP using MATLAB》Problem 8.35
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- 面试系列 30 如何自己设计一个类似dubbo的rpc框架
其实一般问到你这问题,你起码不能认怂,因为既然咱们这个课程是短期的面试突击训练课程,那我不可能给你深入讲解什么kafka源码剖析,dubbo源码剖析,何况我就算讲了,你要真的消化理解和吸收,起码个把月 ...