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

  1. Springboot 2.0.4 整合Mybatis出现异常Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required

    在使用Springboot 2.0.4 整合Mybatis的时候出现异常Property 'sqlSessionFactory' or 'sqlSessionTemplate' are require ...

  2. SpringBoot数据访问之整合mybatis注解版

    SpringBoot数据访问之整合mybatis注解版 mybatis注解版: 贴心链接:Github 在网页下方,找到快速开始文档 上述链接方便读者查找. 通过快速开始文档,搭建环境: 创建数据库: ...

  3. SpringBoot学习- 3、整合MyBatis

    SpringBoot学习足迹 1.下载安装一个Mysql数据库及管理工具,同类工具很多,随便找一个都可以,我在windows下做测试项目习惯使用的是haosql 它内部集成了MySql-Front管理 ...

  4. (入门SpringBoot)SpringBoot项目数据源以及整合mybatis(二)

    1.配置tomcat数据源: #   数据源基本配置spring.datasource.url=jdbc:mysql://localhost:3306/shoptest?useUnicode=true ...

  5. SpringBoot数据访问之整合Mybatis配置文件

    环境搭建以及前置知识回顾 SpringBoot中有两种start的形式: 官方:spring-boot-starter-* 第三方:*-spring-boot-starter Mybatis属于第三方 ...

  6. SpringBoot Maven多模块整合MyBatis 打包jar

    最近公司开始新的项目,框架选定为SpringBoot+Mybatis,本篇主要记录了在IDEA中搭建SpringBoot多模块项目的过程. 源码:https://github.com/12641561 ...

  7. springboot学习四:整合mybatis

    在application.properties加入配置 ## Mybatis 配置 mybatis.typeAliasesPackage=org.spring.springboot.domain my ...

  8. SpringBoot(十)-- 整合MyBatis

    1.pom.xml 配置maven依赖 <dependency> <groupId>org.mybatis.spring.boot</groupId> <ar ...

  9. 三、SpringBoot 整合mybatis 多数据源以及分库分表

    前言 说实话,这章本来不打算讲的,因为配置多数据源的网上有很多类似的教程.但是最近因为项目要用到分库分表,所以让我研究一下看怎么实现.我想着上一篇博客讲了多环境的配置,不同的环境调用不同的数据库,那接 ...

随机推荐

  1. 在Vcl和FireMonkey应用程序中启用TXMLDocument 的XPath(selectNode,selectNodes)方法

    该TXMLDocument的类让你来操作VCL和FireMonkey应用程序的XML文件,但这个类没有实现直接的方式来调用XPath的相关方法(selectNode,的selectNodes),所以你 ...

  2. 客开监控(BE/UI/BP)插件停用与启用

    1.单据界面右键属性,获取当前客开监控页面URL连接:http://172.16.168.15/U9/erp/display.aspx?lnk=UFSoft.UBF.Cust.CustManager& ...

  3. pytest_函数传参和firture传参数request

    前言为了提高代码的复用性,我们在写用例的时候,会用到函数,然后不同的用例去调用这个函数. 比如登录操作,大部分的用例都会先登录,那就需要把登录单独抽出来写个函数,其它用例全部的调用这个登陆函数就行. ...

  4. yum命令安装jdk

    1.查看是否已安装JDK,卸载 yum list installed |grep java java--openjdk.x86_64 :.b13.el7_5 @updates java--openjd ...

  5. 设置elasticsearch的默认分区数和副本数

    日志是从logstash传输给ES的,但是logstash配置中只能配置host和index,所以只能在es中进行配置 但是在es配置文件中配置,也就是新增如下参数的话会报错:node setting ...

  6. hystrix,request collapser,请求合并

    多个商品,需要发送多次网络请求,调用多次接口,才能拿到结果 可以使用HystrixCollapser将多个HystrixCommand合并到一起,多个command放在一个command里面去执行,发 ...

  7. el-select和el-cascader的visible-change下拉框隐藏时触发相关事件(下拉框下拉显示时不触发)

    原文:https://blog.csdn.net/CarryBest/article/details/79959389 今天做项目时,用elementUI框架,需要下拉框隐藏时出发某个函数,用了vis ...

  8. Mycat使用--分库分表和读写分离

    Mycat分库分表读写分离 1. 模拟多数据库节点 2. 配置文件 具体操作参看: https://blog.csdn.net/vbirdbest/article/details/83448757 写 ...

  9. ioremap

    将一个IO地址空间映射到内核的虚拟地址空间上 物理地址 CPU地址总线传来的地址,由硬件电路控制其具体含义.物理地址中很大一部分是留给内存条中内存的,但也常被映射到其他存储器上(如显存.bios等) ...

  10. 理解 Cookie,Session,Token 并结合 Redis 的使用

    Http 协议是一个无状态协议, 客户端每次发出请求, 请求之间是没有任何关系的.但是当多个浏览器同时访问同一服务时,服务器怎么区分来访者哪个是哪个呢? cookie.session.token 就是 ...