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 ...
随机推荐
- iOS音频开发系列-概述篇
概述 iOS中对于音频的处理,苹果提供了两个库. AVFoundation AudioToolbox 在iOS系统中apple对上述的流程进行了封装并提供了不同层次的接口
- lunix查询jdk安装路径
在linux系统查找jdk的安装路径:whereis javawhich java (java执行路径)echo $JAVA_HOME echo $PATH在windows查找jdk的安装路径:set ...
- Random类和Math.random()方法
一.Random类的定义Random类位于 java.util 包中,主要用于生成伪 随机数Random类将 种子数 作为随机算法的起源数字,计算生成伪随机数,其与生成的随机数字的区间无关创建Rand ...
- matlab 求已知概率密度函数的随机数生成
N=10000; %需要随机数的个数 a=zeros(N,1); %存放随机数的数列 n=0; f1=@(t) 1./(1.2*pi*(1+5*(t-7.3).^2)); f2=@(t) 1./(1. ...
- grunt完整的配置demo
const path = require('path') const fs = require('fs'); module.exports = function (grunt) { grunt.reg ...
- Java 学习 时间格式化(SimpleDateFormat)与历法类(Calendar)用法详解
基于Android一些时间创建的基本概念 获取当前时间 方式一: Date date = new Date(); Log.e(TAG, "当前时间="+date); 结果: E/T ...
- ci 连接myssql
由于要将mssql 和 mysql 里面的数据进行对比,So. 配置:database.php $db['default']['hostname'] = '192.168.1.222'; $db['d ...
- thinkphp 默认值输出
我们可以给变量输出提供默认值,例如: 大理石平台厂家 {$user.nickname|default="这家伙很懒,什么也没留下"} 对系统变量依然可以支持默认值输出,例如: {$ ...
- 构造流量图+乱搞——cf990F
/* 结论1:有解的充要条件是所有点权之和为0 结论2:删掉环上的一条边,只要将这个环上的其余边都减去这条边的边权,那么这个图仍是等价的 从原图网络中构造出一棵带权值的树即可,其他边权都设置为0 通过 ...
- Vue+Iview+Node 项目结构和配置
1.项目调整后的目录 api:数据接口定义 assets:静态文件 components:组件 config:项目相关配置 driective:指令 router:路由 store:状态管 ...