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 ...
随机推荐
- Python 面向对象二(转载)
来源:www.cnblogs.com/wupeiqi/p/4766801.html 三.类成员的修饰符 类的所有成员在上一步骤中已经做了详细的介绍,对于每一个类的成员而言都有两种形式: 1.公有成员, ...
- LLVM每日谈之二十 Everything && Clang driver
作者:史宁宁(snsn1984) 近期在读<Getting Started with LLVM Core Libraries>.这是读的第一本LLVM的书.非常多地方尽管讲的是自己知道的东 ...
- 2017.6.8 spring-ldap基本使用总结
之前学习过spring-ldap的官方文档:2017.4.10 spring-ldap官方文档学习 现在是对实际使用的spring-ldap及使用过程中遇到的问题,进行总结. 1.spring-lda ...
- [WCF菜鸟]什么是WCF
一.概述 Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口,可以翻译为Windows通讯接口,它是.NET框架的一部分.由 .NE ...
- 移动web之响应式布局
1.响应式布局的概念 响应式布局是Ethan Marcotte在2010年5月份提出的一个概念.简而言之.就是一个站点可以兼容多个终端--而不是为每一个终端做一个特定的版本号. 这个概念是为解决移动互 ...
- 从git下载一个工程
1.右键点工程区,选“Import...” 2.Import source 选Git->“Projects from Git” 3.三个选择项中选最后一项CloneURI 4.弹出对话框,URI ...
- 100多道经典的JAVA面试题及答案解析
面向对象编程(OOP) Java是一个支持并发.基于类和面向对象的计算机编程语言.下面列出了面向对象软件开发的优点: 代码开发模块化,更易维护和修改. 代码复用. 增强代码的可靠性和灵活性. 增加代码 ...
- git merge 和 git rebase 小结(转)
git merge是用来合并两个分支的. git merge b # 将b分支合并到当前分支 同样 git rebase b,也是把 b分支合并到当前分支 ---------------------- ...
- 命令行添加pod示例
1.创建AlamFireDemo 工程,关闭工程 2.进入到工程目录 执行 pod init 命令 生成 PodFile文件 3.vi PodFile编辑该文件 启用:platform :ios, ' ...
- Oracle 为表空间增加数据文件
dba权限检查下 select tablespace_name, file_id, file_name, ),) total_space from dba_data_files order by ta ...