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. Guarded Suspention 要等到我准备好

    线程在运行过程中需要停下来等待,然后再继续执行. 范例程序,一个线程(ClientThread)对另外一个线程(ServerThread)传递请求,实现一个模拟消息通道的功能. public clas ...

  2. springcloud eureka server 检测 eureka client 状态

    import com.netflix.discovery.shared.Applications; import com.netflix.eureka.EurekaServerContextHolde ...

  3. 2018-10-29-微软-Tech-Summit-技术暨生态大会课程-·-基于-Roslyn-打造高性能预编译框架...

    title author date CreateTime categories 微软 Tech Summit 技术暨生态大会课程 · 基于 Roslyn 打造高性能预编译框架 lindexi 2018 ...

  4. CSS压缩

    通过W3C 统一验证工具的检测没有错误后,为了提高加载速度和节约空间(相对来说,css量很少的情况下,几乎没啥区别),可以通过css压缩工具把css进行压缩. w3c css压缩 http://too ...

  5. thinkphp 切换数据库

    除了在预先定义数据库连接和实例化的时候指定数据库连接外,我们还可以在模型操作过程中动态的切换数据库,支持切换到相同和不同的数据库类型.用法很简单, 只需要调用Model类的db方法,用法: 常州大理石 ...

  6. ie6-8 avalon2 单页应用项目实战备忘

    坑爹的ie,作为小组leader,尼玛,小伙伴儿们不乐意做的事情,我来做好了..心累... 如果,各位同学有定制开发ie6-8版本的需求,还是尽量不要用单页应用模式了,也不要用avalon这类mvvm ...

  7. javaSpring学习总结day_02

    使用注解注入: 1.用于创建bean对象 @Component: 作用:相当于配置了一个bean标签 位置:类上面 属性:value,含义是bean的id,当不写时,有默认值,默认值是当前类的短名,首 ...

  8. 1.关于Python的发展历史你知道吗?

    1989,为了度过圣诞假期,Guido开始编写Python语言编译器.Python这个名字来自Guido的喜爱的电视连续剧<蒙蒂蟒蛇的飞行马戏团>.他希望新的语言Python能够满足他在C ...

  9. GNU GRUB引导的默认启动项是ubuntu

    安装了ubuntu16.04后,GNU GRUB引导的默认启动项是ubuntu,如果希望默认启动项是windows,修改方法如下: step1. 进入Ubuntu系统,打开终端,输入 sudo ged ...

  10. nginx压力测试webbench

    下载压力测试工具webbench wget http://home.tiscali.cz/~cz210552/distfiles/webbench-1.5.tar.gz 安装依赖包 yum -y in ...