1 选择mysql驱动

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency>

2 选择mybatis orm

<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>

3 配置mysql和mybatis

spring.datasource.url=jdbc:mysql://localhost:3306/springbootdb?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

mybatis.mapperLocations=classpath:mapper/*.xml

4 dao dao和Mapper xml

它们是一一对应的,Mapper xml中负责sql的编写,dao负责在业务中执行相应的操作。

package com.hello.springboot.dao;
import com.hello.springboot.entity.User;
import java.util.List;
public interface UserDao {
List<User> findAll();
User findById(Long id);
void insert(User user);
void update(User user);
void delete(Long id);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!--namespace是命名空间,是mapper接口的全路径-->
<mapper namespace="com.hello.springboot.dao.UserDao"> <!--resultMap – 是最复杂也是最强大的元素,用来描述如何从数据库结果集中来加载对象-->
<resultMap id="userResultMap" type="com.hello.springboot.entity.User">
<id property="name" column="username"></id>
</resultMap> <!--sql – 可被其他语句引用的可重用语句块-->
<sql id="colums">
id,username,age,pwd
</sql> <select id="findAll" resultMap="userResultMap">
select
<include refid="colums" />
from user
</select> <select id="findById" resultMap="userResultMap">
select
<include refid="colums" />
from user
where id=#{id}
</select> <insert id="insert" parameterType="com.hello.springboot.entity.User" >
INSERT INTO
user
(username,age,pwd)
VALUES
(#{name}, #{age}, #{pwd})
</insert> <update id="update" parameterType="com.hello.springboot.entity.User" >
UPDATE
users
SET
<if test="username != null">username = #{username},</if>
<if test="pwd != null">pwd = #{pwd},</if>
username = #{username}
WHERE
id = #{id}
</update> <delete id="delete" parameterType="java.lang.Long" >
DELETE FROM
user
WHERE
id =#{id}
</delete> </mapper>

三点要注意:

第一,注意,Mapper xml中的namespace就是对应的dao接口的完整路径名;

第二,Mapper接口中的方法的名字要和Mapper xml中操作的名字一样;

第三,注意参数;

5 在代码中使用dao层的接口

给dao类加上下面的注解,然后就可以像普通的类一样使用dao对象。

@Repository
@Mapper

6 关于dao接口中函数的返回值

第一,调用dao接口时用try catch来进行异常处理,如果出现异常会抛出异常;

第二,在mapper.xml中指定返回值的类型即可,这个类型保持和dao接口中函数的类型一致;

spring boot mysql和mybatis的更多相关文章

  1. spring boot 2使用Mybatis多表关联查询

    模拟业务关系:一个用户user有对应的一个公司company,每个用户有多个账户account. spring boot 2的环境搭建见上文:spring boot 2整合mybatis 一.mysq ...

  2. spring boot 2整合mybatis

    mybatis-spring-boot-starter主要有两种解决方案,一种是使用注解,一种是使用XML. 参考这篇文章动手跑了一个例子,稍微不同之处,原文是spring boot,这里改成了spr ...

  3. Spring Boot 中使用 MyBatis 整合 Druid 多数据源

    2017 年 10 月 20 日   Spring Boot 中使用 MyBatis 整合 Druid 多数据源 本文将讲述 spring boot + mybatis + druid 多数据源配置方 ...

  4. Spring Boot:整合MyBatis框架

    综合概述 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简单 ...

  5. Spring Boot:实现MyBatis分页

    综合概述 想必大家都有过这样的体验,在使用Mybatis时,最头痛的就是写分页了,需要先写一个查询count的select语句,然后再写一个真正分页查询的语句,当查询条件多了之后,会发现真的不想花双倍 ...

  6. Spring Boot:实现MyBatis动态数据源

    综合概述 在很多具体应用场景中,我们需要用到动态数据源的情况,比如多租户的场景,系统登录时需要根据用户信息切换到用户对应的数据库.又比如业务A要访问A数据库,业务B要访问B数据库等,都可以使用动态数据 ...

  7. Spring Boot:实现MyBatis动态创建表

    综合概述 在有些应用场景中,我们会有需要动态创建和操作表的需求.比如因为单表数据存储量太大而采取分表存储的情况,又或者是按日期生成日志表存储系统日志等等.这个时候就需要我们动态的生成和操作数据库表了. ...

  8. Spring Boot中使用MyBatis注解配置详解(1)

    之前在Spring Boot中整合MyBatis时,采用了注解的配置方式,相信很多人还是比较喜欢这种优雅的方式的,也收到不少读者朋友的反馈和问题,主要集中于针对各种场景下注解如何使用,下面就对几种常见 ...

  9. FW: How to use Hibernate Lazy Fetch and Eager Fetch Type – Spring Boot + MySQL

    原帖 https://grokonez.com/hibernate/use-hibernate-lazy-fetch-eager-fetch-type-spring-boot-mysql In the ...

随机推荐

  1. mysql关于访问权限以及root密码修改

    root密码修改:mysql> use mysql;mysql> UPDATE user SET Password = PASSWORD('newpass') WHERE user = ' ...

  2. Angular 学习笔记——标签指令

    <!DOCTYPE html> <html lang="en" ng-app="myApp"> <head> <met ...

  3. Angular 学习笔记——ng-repeat 隔行换色

    <!DOCTYPE html> <html lang="en" ng-app="myApp"> <head> <met ...

  4. Angular 学习笔记——shop

    <!DOCTYPE html> <html lang="en" ng-app> <head> <meta charset="UT ...

  5. javascript - 活动倒计时(天、时、分、秒)

    计数时: 结束时: 示例: <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...

  6. Easy way to change collation of all database objects in SQL Server

    This info is from: http://www.codeproject.com/Articles/302405/The-Easy-way-of-changing-Collation-of- ...

  7. JavaScript创建块级作用域

    1.JavaScript创建块级作用域 (1)方法一:ES6 (2)方法二:闭包 2.示例 <!DOCTYPE html> <html lang="zh"> ...

  8. JDBC技术总结(三)

    1. 数据库连接池 JDBC部分的前两个总结主要总结了一下JDBC的基本操作,而且有个共同点,就是应用程序都是直接获取数据库连接的.这会有个弊端:用户每次请求都需要向数据库获得连接,而数据库创建连接通 ...

  9. float数据在内存中的存储方法

    浮点型变量在计算机内存中占用4字节(Byte),即32-bit.遵循IEEE-754格式标准.一个浮点数由2部分组成:底数m 和 指数e.                          ±mant ...

  10. Python Windows文件操作

    获得目录和文件名 os.getenv()获取环境变量 os.putenv()设置环境变量 os.getcwd() 获得当前目录 os.chdir(‘要设置的当前目录’) os.listdir() 返回 ...