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整合的更多相关文章

  1. spingBoot整合mybatis+generator+pageHelper

    spingBoot整合mybatis+generator+pageHelper 环境/版本一览: 开发工具:Intellij IDEA 2018.1.4 springboot: 2.0.4.RELEA ...

  2. 【SpringBoot】SpingBoot整合AOP

    https://blog.csdn.net/lmb55/article/details/82470388 [SpringBoot]SpingBoot整合AOPhttps://blog.csdn.net ...

  3. spring+mybatis+oracle/mysql整合开发需要的jar包详解

    导入spring,mybatis,c3p0,oracle和mybatis提供的与spring整合的插件包   mysql的jar:         mysql-connector-java-5.1.7 ...

  4. MyBatis学习(四)MyBatis和Spring整合

    MyBatis和Spring整合 思路 1.让spring管理SqlSessionFactory 2.让spring管理mapper对象和dao. 使用spring和mybatis整合开发mapper ...

  5. Mybatis与Spring整合,使用了maven管理项目,作为初学者觉得不错,转载下来

    转载自:http://www.cnblogs.com/xdp-gacl/p/4271627.html 一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: mvn archetype ...

  6. spring与mybatis三种整合方法

    spring与mybatis三种整合方法 本文主要介绍Spring与Mybatis三种常用整合方法,需要的整合架包是mybatis-spring.jar,可通过链接 http://code.googl ...

  7. Mybatis+struts2+spring整合

    把student项目改造成ssm  struts2 +mybatis+spring 1,先添加spring支持:类库三个,applicationContext.xml写在webinf下四个命名空间,监 ...

  8. mybatis与spring整合(基于配置文件)

    本文主要介绍了如何将mybatis和spring整合在一起使用,本人使用的是mybatis3.05 + spring3.1.0M2 ,使用dbcp作为数据库连接池. 1.编写数据访问接口(UserDa ...

  9. mybatis与spring整合时读取properties问题的解决

    在学习mybatis与spring整合是,想从外部引用一个db.properties数据库配置文件,在配置文件中使用占位符进行引用,如下: <context:property-placehold ...

随机推荐

  1. JS对象 神奇的Math对象,提供对数据的数学计算。注意:Math 对象是一个固有的对象,无需创建它,直接把 Math 作为对象使用就可以调用其所有属性和方法。这是它与Date,String对象的区别

    Math对象 Math对象,提供对数据的数学计算. 使用 Math 的属性和方法,代码如下: <script type="text/javascript"> var m ...

  2. 神经网络 (2)- Alexnet Training on MNIST

    文章目录 Win10 Anaconda下配置tensorflow+jupyter notebook环境 AlexNet 识别MNIST Win10 Anaconda下配置tensorflow+jupy ...

  3. sql (3) where

    WHERE 子句用于规定选择的标准.语法SELECT 列名称 FROM 表名称 WHERE 列 运算符 值 引号的使用请注意,我们在例子中的条件值周围使用的是单引号. SQL 使用单引号来环绕文本值( ...

  4. soj116 快乐串

    题意:定义一个串是k-happy的:对于所有的Ai,都有Aj(j!=i),使得|Ai-Aj|<=k. 问使得原串至少存在一个长度>=m的连续子串是k-happy的最小的k? 标程: #in ...

  5. Nginx配置两份日志记录

    nginx配置 版本-1.4.4 --- access_log /alidata/log/nginx/access/wordpress1.log ; access_log /alidata/log/n ...

  6. Java 多线程 - synchronized与Lock的区别

    https://blog.csdn.net/qq_39521554/article/details/81130442 http://www.cnblogs.com/huangbw/p/8516024. ...

  7. ThinkPHP角色控制时的错误

    1.Table 'think.think_user' doesn't exist  等的原因是因为'DB_PREFIX' => 'think_', // 数据库表前缀没有配置好,在使用角色控制时 ...

  8. VC++ MFC文件的移动复制删除更名遍历操作

    1.判断文件是否存在 利用CFile类和CFileStatus类判断 CFileStatus filestatus; if (CFile::GetStatus(_T("d://softist ...

  9. dart中extends、 implements、with的用法与区别

    一.概述 继承(关键字 extends) 混入  mixins (关键字 with) 接口实现(关键字 implements) 这三种关系可以同时存在,但是有前后顺序: extends -> m ...

  10. day22_1-课前上节复习+os模块

    # ********************day22_1-课前上节复习+os模块 *******************# ********************day22_1-课前上节复习+os ...