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. C++ 的多继承与虚继承

    C++之多继承与虚继承   1. 多继承 1.1 多继承概念 一个类有多个直接基类的继承关系称为多继承 多继承声明语法 class 派生类名 : 访问控制 基类名1, 访问控制 基类名2, ... { ...

  2. 【SpringBoot】SpingBoot整合AOP

    https://blog.csdn.net/lmb55/article/details/82470388 [SpringBoot]SpingBoot整合AOPhttps://blog.csdn.net ...

  3. centos7划分vlan

    1. lsmod|grep 8021q  确认内核是够载入了802.1q模组 2.modprobe -a 8021q   如果没载入使用这个命令载入模组 3.配置vlan需要vconfig命令,由于c ...

  4. 洛谷【P1048 采药】题解

    题目链接 分析:典型的01背包问题,设dp[i][j]为空间(也就是题面中的时间)是j的背包在装前i个物品(草药)所得的最大价值,v[i]为第i个物品的重量(采药的时间),w[i]为第i个物品(草药) ...

  5. eclipse中启动tomcat后, 无法访问localhost:8080

    问题: 今天老师讲了Servlet路径问题, 做了个测试在eclipse中启动tomcat后,在浏览器地址栏输入 http://localhost8080无法访问, 提示404错误, 正常情况是可以访 ...

  6. JS中的七大数据类型

    js中有7种数据类型,包括五种基本数据类型(Number,String,Boolean,Undefined,Null),和一种复杂数据类型(Object)以及es6语法新增的Symbol数据类型 es ...

  7. 关于ABViewer的疑问解答

    很多 CAD小伙伴都对 ABViewer 这款软件不陌生吧.ABViewer 是用来处理图纸和工程文档管理的一款通用软件.可以用它来查看,编辑,转换,测量和打印DWG和其他CAD文件,以及3D模型和光 ...

  8. 归并排序python实现源码

    将开发过程经常用到的一些代码片段收藏起来,下面的资料是关于归并排序python实现的代码,应该能对码农们有一些用. def mergesort(arr): if len(arr) == 1: retu ...

  9. 解决pycharm导入pygame包问题

    在正确用pip命令安装好pygame后, 运行pycharm仍然会报错找不到pygame包, 这时候我们可以用pycharm自带的工具安装好pygame包,操作步骤如下: 点击设置,(或者ctrl+a ...

  10. jquery 子元素 后代元素 兄弟元素 相邻元素

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content ...