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 ...
随机推荐
- luaj使用 方法签名规则 Cocos2dxLuaJavaBridge
function AndroidHandler:getParamJson() local args = {nil} local ok,ret = luaj.callStaticMeth ...
- Python 读取本地*.txt文件 替换 内容 并保存
# r 以只读的方式打开文件,文件的描述符放在文件的开头# w 打开一个文件只用于写入,如果该文件已经存在会覆盖,如果不存在则创建新文件 #路径path = r"D:\pytho ...
- Java 基础 - final vs static
总结 共同点,都可以修饰类,方法,属性.而不同点: static 含义:表示静态或全局,被修饰的属性和方法属于类,可以用类名.静态属性 / 方法名 访问 static 方法:只能被static方法覆盖 ...
- SQL Server SQLFetch()
{ /* 摘要 SQLFetch从结果集中提取下一个数据行集, 并返回所有绑定列的数据. 语法 C++ SQLRETURN SQLFetch( SQLHSTMT StatementHandle ...
- inspect模块的使用
一.介绍 inspect模块主要的四种用处: 1.对是否是模块.框架.函数等进行类型检测 2.获取源码 3.获取类或函数的参数信息 4.解析堆栈 二.使用 只写了2个自己用到的方法,方法太用,http ...
- mvc和mvvm区别
mvc和mvvm区别 MVC和MVVM的区别其实并不大.都是一种设计思想. 主要就是MVC中Controller演变成MVVM中的viewModel. MVVM主要解决了MVC中大量的DOM操作使页面 ...
- Windows便筏快捷键
Ctrl + L:左对齐 Ctrl + E:居中对齐 Ctrl + R:右对齐 Ctrl + B:加粗 Ctrl + I:斜体 Ctrl + U:给文字添加下划线 Ctrl + T: 给文字添加删除线 ...
- DEVO 7E遥控器配对
先把遥控器上电,并把模型里面的固定ID关闭,放在一旁. 接收器断电,按住CLEAN按钮后上电,会发现接收器慢闪两下后松开. 这时遥控器应该就连上接收器了,这时接收器常亮. 再自行配置固定ID即可.
- PAT甲级——A1124 Raffle for Weibo Followers
John got a full mark on PAT. He was so happy that he decided to hold a raffle(抽奖) for his followers ...
- spring mvc文件上传报错:Expected MultipartHttpServletRequest: is a MultipartResolver configured?
报错原因:spring-mvc.xml 的配置文件中,配置文件上传id不为 “multipartResolver” 解决:id 改为 “multipartResolver”