(一)搭建自己的SpringBoot后台框架整合MyBatis
一:通过idea工具构建基础框架
1. 打开idea,左上角File→New→Project,
2. 点击Next
3. 点击Next,配置如下图,这里我们选择数据库MySQL和持久层框架MyBatis

4. 点击Next,选择工作目录,点击Finish,开始构建
5. 创建完成后,项目目录结构如下

二:配置数据库信息
在application.properties文件中添加如下数据库配置
spring.datasource.url=jdbc:mysql://localhost:3306/demo?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&autoReconnect=true&failOverReadOnly=false
spring.datasource.username=数据库用户名
spring.datasource.password=数据库密码
spring.datasource.driverClassName=com.mysql.jdbc.Driver
三:创建数据库UserInfo表
CREATE TABLE `user_info` (
`id` int(32) NOT NULL AUTO_INCREMENT,
`user_name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
四:创建项目基本目录结构

Model 存放实体类
package com.example.demo.model; import javax.persistence.Column;
import javax.persistence.Id; /**
* @author
* @Description:
* @time 2018/4/18 11:55
*/
public class UserInfo { /**
* 主键
*/
@Id
private String id; /**
* 用户名
*/
@Column(name = "user_name")
private String userName; private String password; public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} }
Mapper
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.dao.UserInfoMapper">
<resultMap id="BaseResultMap" type="com.example.demo.model.UserInfo">
<id column="id" jdbcType="INTEGER" property="id"/>
<result column="user_name" jdbcType="VARCHAR" property="userName"/>
</resultMap> <sql id="Base_Column_List">
id,user_name
</sql> <select id="selectById" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from user_info
where id = #{id,jdbcType=VARCHAR}
</select> </mapper>
DAO层
package com.example.demo.dao; import com.example.demo.model.UserInfo;
import org.apache.ibatis.annotations.Param; /**
* @author
* @Description:
* @time 2018/4/18 11:54
*/
public interface UserInfoMapper { UserInfo selectById(@Param("id") Integer id);
}
Service
package com.example.demo.service; import com.example.demo.model.UserInfo; /**
* @author
* @Description:
* @time 2018/4/18 11:56
*/
public interface UserInfoService { UserInfo selectById(Integer id); }
ServiceImpl
package com.example.demo.service.impl; import com.example.demo.dao.UserInfoMapper;
import com.example.demo.model.UserInfo;
import com.example.demo.service.UserInfoService;
import org.springframework.stereotype.Service; import javax.annotation.Resource; /**
* @author
* @Description:
* @time 2018/4/18 11:56
*/
@Service
public class UserInfoServiceImpl implements UserInfoService{ @Resource
private UserInfoMapper userInfoMapper; public UserInfo selectById(Integer id){
return userInfoMapper.selectById(id);
}
}
Controller
package com.example.demo.controller; import com.example.demo.model.UserInfo;
import com.example.demo.service.UserInfoService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; /**
* @author
* @Description:
* @time 2018/4/18 11:39
*/
@RestController
@RequestMapping("userInfo")
public class UserInfoController { @Resource
private UserInfoService userInfoService; @PostMapping("/hello")
public String hello(){
return "hello SpringBoot";
} @PostMapping("/selectById")
public UserInfo selectById(Integer id){
return userInfoService.selectById(id);
}
}
MyBatis 的配置Java类
package com.example.demo.core.configurer; import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver; import javax.sql.DataSource; /**
* @ClassName: MybatisConfigurer
* @Description: Mybatis配置
* @author
* @date 2018年1月20日 下午4:03:46
*
*/
@Configuration
public class MybatisConfigurer { @Bean
public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception {
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
factory.setDataSource(dataSource);
factory.setTypeAliasesPackage("com.example.demo.model");
// 添加XML目录
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
factory.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
return factory.getObject();
} @Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean");
mapperScannerConfigurer.setBasePackage("com.example.demo.dao");
return mapperScannerConfigurer;
}
}
@Configuration表示该文件是一个配置文件
@Bean表示该方法是一个传统xml配置文件中的<Bean id=""></Bean>
其中factory.setTypeAliasesPackage("com.example.demo.model")表示项目中model的存储路径;
factory.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));表示mapper.xml存储路径;
mapperScannerConfigurer.setBasePackage("com.example.demo.dao");表示dao层的存储路径
五:运行项目
找到DemoApplication,右键,选择run DemoApplication
以上内容来源网上,如有侵权请联系本人!!!
(一)搭建自己的SpringBoot后台框架整合MyBatis的更多相关文章
- springboot 后台框架平台 mybatis 集成代码生成器 shiro 权限 websocket
1.代码生成器: [正反双向](单表.主表.明细表.树形表,快速开发利器)freemaker模版技术 ,0个代码不用写,生成完整的一个模块,带页面.建表sql脚本.处理类.service等完整模块2. ...
- 【springboot spring mybatis】看我怎么将springboot与spring整合mybatis与druid数据源
目录 概述 1.mybatis 2.druid 壹:spring整合 2.jdbc.properties 3.mybatis-config.xml 二:java代码 1.mapper 2.servic ...
- SpringBoot 2.X整合Mybatis
1.创建工程环境 勾选Web.Mybatis.MySQL,如下 依赖如下 <dependency> <groupId>org.springframework.boot</ ...
- 利用IDEA搭建SpringBoot项目,整合mybatis
一.配置文件.启动项目 生成之后这几个文件可以删掉的 配置application spring.datasource.url=jdbc:mysql://localhost:3306/test?serv ...
- SpringBoot学习之整合Mybatis
本博客使用IDEA开发工具,通过Maven构建SpringBoot项目,初始化项目添加的依赖有:spring-boot-starter-jdbc.spring-boot-starter-web.mys ...
- SpringBoot | 3.2 整合MyBatis
目录 前言 1. 导入MyBatis场景 1.1 初始化导向 1.2 手动导入 2. *MyBatis自动配置原理 3. 全局配置文件 @Mapper @MapperScan 3.1 配置模式 3.2 ...
- SpringBoot当中如何整合mybatis和注入
[学习笔记] 6.整合mybatis和注入: 马克-to-win@马克java社区: 根据第3部分的helloworld例子,用那个项目做底子.pom.xml只需要加入mybatis和mysql的部分 ...
- spring 框架整合mybatis的源码分析
问题:spring 在整合mybatis的时候,我们是看不见sqlSessionFactory,和sqlsession(sqlsessionTemplate 就是sqlsession的具体实现)的,这 ...
- springboot笔记07——整合MyBatis
前言 Springboot 整合 MyBatis 有两种方式,分别是:"全注解版" 和 "注解.xml混合版". 创建项目 创建Springboot项目,选择依 ...
随机推荐
- js团购倒计时函数代码
<h1>团购啦!</h1><p>还剩<span id="times"></span></p> <SCR ...
- 面试-1-C#浅解
面试-1 C#浅解众所周知c#是微软推出的一款完全没面向对象的编程语言,那么对象是什么?在现实生活中人们一提到对象首先想到的就是“情侣”!但是在我们的程序中对象是什么? 在程序中个能够区别于其他事 ...
- Computer form factor
http://en.wikipedia.org/wiki/Motherboard_form_factor Computer form factor From Wikipedia, the free e ...
- HashMap底层原理以及与ConCurrentHashMap区别
HashMap基于hashing原理,我们通过put()和get()方法储存和获取对象.当我们将键值对传递给put()方法时,它调用键对象的hashCode()方法来计算hashcode,让后找到bu ...
- ln: 正在创建指向“asm-arm”的符号链接“asm”: 不支持的操作
原因是不能在windows共享目录编译,将待编译的uboot源码copy到home目录
- 豆瓣面试题strstr)
/*(豆瓣2013面试题strstr) 有一个在给定字符串中查找子串的函数strstr, 该函数从给定的字符串src中查找substr并返回一个整数, 指明substr第一次出现的位置(从0开始计数) ...
- mysql关闭skip-grant-tables快速重置mysql密码
如果你忘记了mysql密码几乎是没有什么好办法可以直接修改密码了,但我们可以在my.ini把加上skip-grant-tables,然后重启mysql就不需要密码了,这时我们再修改root密码,最后再 ...
- spark0.9.1集群模式执行graphx測试程序(LiveJournalPageRank,新增Connected Components)
spark最新版公布了.之前的版本号就已经集成了graphx,这个版本号还改了一些bug. 我做了简单測试,只是网上关于集群模式执行spark资料太少了,仅仅有关于EC2(见參考资料1)的.可是还非常 ...
- POJ 1703 Find them, Catch them(种类并查集)
题目链接 这种类型的题目以前见过,今天第一次写,具体过程,还要慢慢理解. #include <cstring> #include <cstdio> #include <s ...
- React创建组件的三种方式比较和入门实例
推荐文章: https://www.cnblogs.com/wonyun/p/5930333.html 创建组件的方式主要有: 1.function 方式 2.class App extends Re ...