spring boot mysql和mybatis
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的更多相关文章
- spring boot 2使用Mybatis多表关联查询
模拟业务关系:一个用户user有对应的一个公司company,每个用户有多个账户account. spring boot 2的环境搭建见上文:spring boot 2整合mybatis 一.mysq ...
- spring boot 2整合mybatis
mybatis-spring-boot-starter主要有两种解决方案,一种是使用注解,一种是使用XML. 参考这篇文章动手跑了一个例子,稍微不同之处,原文是spring boot,这里改成了spr ...
- Spring Boot 中使用 MyBatis 整合 Druid 多数据源
2017 年 10 月 20 日 Spring Boot 中使用 MyBatis 整合 Druid 多数据源 本文将讲述 spring boot + mybatis + druid 多数据源配置方 ...
- Spring Boot:整合MyBatis框架
综合概述 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简单 ...
- Spring Boot:实现MyBatis分页
综合概述 想必大家都有过这样的体验,在使用Mybatis时,最头痛的就是写分页了,需要先写一个查询count的select语句,然后再写一个真正分页查询的语句,当查询条件多了之后,会发现真的不想花双倍 ...
- Spring Boot:实现MyBatis动态数据源
综合概述 在很多具体应用场景中,我们需要用到动态数据源的情况,比如多租户的场景,系统登录时需要根据用户信息切换到用户对应的数据库.又比如业务A要访问A数据库,业务B要访问B数据库等,都可以使用动态数据 ...
- Spring Boot:实现MyBatis动态创建表
综合概述 在有些应用场景中,我们会有需要动态创建和操作表的需求.比如因为单表数据存储量太大而采取分表存储的情况,又或者是按日期生成日志表存储系统日志等等.这个时候就需要我们动态的生成和操作数据库表了. ...
- Spring Boot中使用MyBatis注解配置详解(1)
之前在Spring Boot中整合MyBatis时,采用了注解的配置方式,相信很多人还是比较喜欢这种优雅的方式的,也收到不少读者朋友的反馈和问题,主要集中于针对各种场景下注解如何使用,下面就对几种常见 ...
- 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 ...
随机推荐
- pip install 报错UnicodeDecodeError: 'ascii' codec can't decode byte
2017-03-23 报错原因: pip安装Python包会加载我的用户目录,我的用户目录恰好是中文的,ascii不能编码. 解决办法: python目录 Python27\Lib\site-pack ...
- 在Hadoop监控页面怎样查看Hive的完整SQL
如图.这里仅仅能看到简单的一段SQL.差点儿看不出详细在运行什么任务. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGFvemhhb2t1bg==/fo ...
- oc 之中的 汉字字符串转化成为拼音 汉字字符串的排序
在oc 之中的字符串为汉字的时候,我们经常要进行字符串比較,可是汉字不能比較,所以就要将汉字转化成为拼音,详细步骤例如以下: //可变字符串 必须是可变字符串. NSMutableString ...
- 系统封装 EasyBoot如何将WIN7安装版提取到光盘
1 将WIN7光盘中的文件提取到Easyboot根目录,注意不要autorun.inf和setup.exe这两个文件.我们这里的Easyboot已经有了一些其他东西(XP的安装版文件,PE的文件等等, ...
- Laravel之加密解密/日志/异常处理及自定义错误
一.加密解密 1.加密Crypt::encrypt($request->secret) 2.解密try { $decrypted = Crypt::decrypt($encryptedValue ...
- C++Singleton的DCLP(双重锁)实现以及性能测评
本文系原创,转载请注明:http://www.cnblogs.com/inevermore/p/4014577.html 根据维基百科,对单例模式的描述是: 确保一个类只有一个实例,并提供对该 ...
- STL源代码剖析 容器 stl_list.h
本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie list ----------------------------------------- ...
- Python魔法师
第一章:数据结构和算法 1.1 查找最大或者最小的n个元素 heapq 模块的两个函数 nlargest() nsmallest() import heapq nums = [1, 8, 2, 23 ...
- python——iterator迭代器|iterator详解——20140918|
-----------------------------------------------------------------------------前言--------------------- ...
- CHM乱码解决方案!
--希望对您有所帮助,闲暇之余请访问我的网店高清数据线 下载了一个日文的chm帮助文件,打开的时候,发现是乱码, CHM 文档不像IE浏览器一样,右键可以选择字符编码,非常不便. 原因可能是 CHM ...