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. spring boot 引用外部配置文件

    java -jar xx.jar -Dspring.config.location=/data/apps/xx/application-prod.properties

  2. 利用yarn多队列实现hadoop资源隔离

    大数据处理离不开hadoop集群的部署和管理,对于本来硬件资源就不多的创业团队来说,做好资源的共享和隔离是很有必要的,毕竟不像BAT那么豪,那么怎么样能把有限的节点同时分享给多组用户使用而且互不影响呢 ...

  3. 解决The prefix 'context' for element 'context:component-scan' is not bound

    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w ...

  4. [转] docker rmi命令-删除image

    原文:http://www.simapple.com/341.html ---------------------------------------------------------------- ...

  5. Tomcat、Weblogic、JBoss、GlassFish、Resin、Websphere弱口令及拿webshell方法总结 [复制链接]

    1.java应用服务器    Java应用服务器主要为应用程序提供运行环境,为组件提供服务.Java 的应用服务器很多,从功能上分为两类:JSP 服务器和 Java EE 服务器.1.1  常见的Se ...

  6. 使用 react-native-tab-navigator 创建 TabBar 组件

    1.首先安装好ReactNative的运行环境,安装组件依赖库 使用npm install react-native-tab-navigator --save安装所依赖的第三方库 2.导入 impor ...

  7. Python的Django框架中的Cookie相关处理

    Python的Django框架中的Cookie相关处理 浏览器的开发人员在非常早的时候就已经意识到. HTTP's 的无状态会对Web开发人员带来非常大的问题,于是(cookies)应运而生. coo ...

  8. webcat——基于netty的http和websocket框架

    代码地址如下:http://www.demodashi.com/demo/12687.html Webcat是一个基于netty的简单.高性能服务端框架,目前提供http和websocket两种协议的 ...

  9. 如何在 Linux 中找到你的 公网IP 地址

    每个网站都有一个独有的公开 IP 地址,可供任何人从任何地方访问. 互联网协议Internet Protocol(IP)不需要介绍 —— 我们每天都在使用它.即使你不直接使用它,当你在浏览器上输入 w ...

  10. 文件json

    import jsondef op_data(filename,dic=None): if dic:#写入进去 with open(filename,'w',encoding='utf-8') as ...