SpringBoot第二节(SpringBoot整合Mybatis)
1.创建Spring Initiallizr项目

一直点击下一步
2.引入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency> <!--引入springboot-mybatis的依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!--MySQL的依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
3.创建resources文件夹

4.编写application.properties
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql:///invoicingsystem
spring.datasource.driver-class-name=com.mysql.jdbc.Driver mybatis.mapper-locations=mapper/*.xml
mybatis.type-aliases-package=com.example.entity

application.properties文件里前缀为Spring.datasource 以及一些包的名字
5.编写Users实体类


6.创建IUsersDao接口
@Repository("iUsersDao")
public interface IUsersDao {
//登录
Users getlogin(@Param("userName") String userName, @Param("password") String password);
//查看
List<Users> getAll();
}
7.编写mapper xml文件
<?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">
<!--namespace需要指向接口全路径-->
<mapper namespace="com.example.dao.IUsersDao">
<!--登录-->
<select id="getlogin" resultType="com.example.entity.Users">
SELECT * FROM users WHERE userName=#{userName} AND PASSWORD=#{password}
</select> <!--查询用户-->
<select id="getAll" resultType="Users">
select * from users
</select>
</mapper>
8.编写IUsersService
public interface IUsersService {
//登录
Users getlogin(String userName, String password);
//查看
List<Users> getAll();
}
9.编写IUsersServiceImpl实现类
@Service("iUsersService")
public class IUsersServiceImpl implements IUsersService {
@Resource(name = "iUsersDao")
private IUsersDao iUsersDao;
@Override
public Users getlogin(String userName, String password) {
return iUsersDao.getlogin(userName,password);
}
@Override
@Transactional
public List<Users> getAll() {
return iUsersDao.getAll();
}
}
9.创建App启动类
@SpringBootApplication
@MapperScan("com.example.dao")
public class DemoApplication { public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
} }

10.启动运行

补充:
在项目中application.properties还有另一种写法
application.yml
spring:
datasource:
username: root #必须以空格间隔
password: root
url: jdbc:mysql://localhost:3307/countrydb
driver-class-name: com.mysql.jdbc.Driver mybatis:
mapper-locations: mapper/*.xml
type-aliases-package: com.cmy.entity
SpringBoot第二节(SpringBoot整合Mybatis)的更多相关文章
- Springboot 2.0.4 整合Mybatis出现异常Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
在使用Springboot 2.0.4 整合Mybatis的时候出现异常Property 'sqlSessionFactory' or 'sqlSessionTemplate' are require ...
- SpringBoot数据访问之整合mybatis注解版
SpringBoot数据访问之整合mybatis注解版 mybatis注解版: 贴心链接:Github 在网页下方,找到快速开始文档 上述链接方便读者查找. 通过快速开始文档,搭建环境: 创建数据库: ...
- SpringBoot学习- 3、整合MyBatis
SpringBoot学习足迹 1.下载安装一个Mysql数据库及管理工具,同类工具很多,随便找一个都可以,我在windows下做测试项目习惯使用的是haosql 它内部集成了MySql-Front管理 ...
- (入门SpringBoot)SpringBoot项目数据源以及整合mybatis(二)
1.配置tomcat数据源: # 数据源基本配置spring.datasource.url=jdbc:mysql://localhost:3306/shoptest?useUnicode=true ...
- SpringBoot数据访问之整合Mybatis配置文件
环境搭建以及前置知识回顾 SpringBoot中有两种start的形式: 官方:spring-boot-starter-* 第三方:*-spring-boot-starter Mybatis属于第三方 ...
- SpringBoot Maven多模块整合MyBatis 打包jar
最近公司开始新的项目,框架选定为SpringBoot+Mybatis,本篇主要记录了在IDEA中搭建SpringBoot多模块项目的过程. 源码:https://github.com/12641561 ...
- springboot学习四:整合mybatis
在application.properties加入配置 ## Mybatis 配置 mybatis.typeAliasesPackage=org.spring.springboot.domain my ...
- SpringBoot(十)-- 整合MyBatis
1.pom.xml 配置maven依赖 <dependency> <groupId>org.mybatis.spring.boot</groupId> <ar ...
- 三、SpringBoot 整合mybatis 多数据源以及分库分表
前言 说实话,这章本来不打算讲的,因为配置多数据源的网上有很多类似的教程.但是最近因为项目要用到分库分表,所以让我研究一下看怎么实现.我想着上一篇博客讲了多环境的配置,不同的环境调用不同的数据库,那接 ...
随机推荐
- 网络爬虫第五章之Scrapy框架
第一节:Scrapy框架架构 Scrapy框架介绍 写一个爬虫,需要做很多的事情.比如:发送网络请求.数据解析.数据存储.反反爬虫机制(更换ip代理.设置请求头等).异步请求等.这些工作如果每次都要自 ...
- Linux下嵌入式Web服务器BOA和CGI编程开发
**目录**一.环境搭建二.相关配置(部分)三.调试运行四.测试源码参考五.常见错误六.扩展(CCGI,SQLite) # 一.环境搭建操作系统:Ubuntu12.04 LTSboa下载地址(但是我找 ...
- zookeeper+kafka集群的安装
时效性要求很高的数据,库存,采取的是数据库+缓存双写的技术方案,也解决了双写的一致性的问题 缓存数据生产服务,监听一个消息队列,然后数据源服务(商品信息管理服务)发生了数据变更之后,就将数据变更的消息 ...
- .Net MVC如何渲染带有网页标签的字符串
有时候我们在解析一段文字时,可能文字中会包含网页上的标签,如div.p等等.那么如果将这种文字渲染成对应的标签效果呢?如图,最近博主就拿到了这么一段字符串(如图) 由于中间带有很多特殊字符,用Html ...
- 混沌理论(Chaos theory)和非线性系统
混沌理论(Chaos theory)是关于非线性系统在一定参数条件下展现分岔(bifurcation).周期运动与非周期运动相互纠缠,以至于通向某种非周期有序运动的理论.在耗散系统和保守系统中,混沌运 ...
- vue mixins是什么及应用
mixins是什么? 官网对此的解释比较文绉绉,通俗的理解很简单,就是提供功能抽象 如A,B,C ...Z等很多个页面用到同一个功能,此时的做法就应该把该功能抽象出来,mixins就是干这个的 当然, ...
- redis设置远程连接
1.修改redis服务器的配置文件 本机安装的redis-4.0.14默认的配置文件 redis.conf 设置 绑定本机地址:bind 127.0.0.1 开启保护模式:protected-mode ...
- E2E测试工具之--01 Cypress 上手使用
The web has evolved. Finally, testing has too. 1. 简介 cypress 最近很火的e2e(即end to end(端到端))测试框架,它基于node ...
- ssm的maven项目启动tomcat时报错,Cannot find class: XXXX解决办法
最近在写一个ssm的项目,启动总是报错.原因网上查了也没找到.最后终于解决.下面直接上代码 问题描述: 严重: Allocate exception for servlet ssm-dispatche ...
- Mysql5.7 建表报 [Err] 1055 问题
最近,在win10系统上,使用docker下载了 mysql5.7镜像,然后建表时,发生奇怪的问题,表正常创建,但底部会出现一行错误信息,如下: [Err] 1055 - Expression #1 ...