网站建设 新闻,一键安装wordpress,营销策划书怎么写格式,零基础能学wordpress吗## 一、简介
1.1 MyBatis介绍
MyBatis 是一款优秀的持久层框架#xff0c;它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC代码和手动设置参数以及获取结果集。
1.2 MyBatis发展史
MyBatis 原本是apache的一个开源项目iBatis, 2010年这个项目由ap…## 一、简介
1.1 MyBatis介绍
MyBatis 是一款优秀的持久层框架它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC代码和手动设置参数以及获取结果集。
1.2 MyBatis发展史
MyBatis 原本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code并且改名为MyBatis 2013年11月迁移到Github。 1.3 MyBatis和Hibernate的区别
MyBatis 和 Hibernate 都是优秀的持久化框架都支持JDBCJava DataBase Connection和JTAJava Transaction API事务处理。
MyBatis 优点
更加轻量级如果说Hibernate是全自动的框架MyBatis就是半自动的框架入门简单即学即用并且延续了很好的SQL使用经验
Hibernate 优点
开发简单、高效不需要编写SQL就可以进行基础的数据库操作可移植行好大大降低了MySQL和Oracle之间切换的成本因为使用了HQL查询而不是直接写SQL语句缓存机制上Hibernate也好于MyBatis
1.4 MyBatis集成方式
Mybatis集成方式分为两种
注解版集成XML版本集成
XML版本为老式的配置集成方式重度集成XML文件SQL语句也是全部写在XML中的注解版版本相对来说比较简约不需要XML配置只需要使用注解和代码来操作数据。
二、注解版 MyBatis 集成
开发环境
MySQL 8.0.12Spring Boot 2.0.4MyBatis Spring Boot 1.3.2等于 MyBatis 3.4.6JDK 8IDEA 2018.2
MyBatis Spring Boot 是 MyBatis 官方为了集成 Spring Boot 而推出的MyBatis版本。
2.1 添加依赖
设置pom.xml文件添加如下配置
dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.12/version
/dependencydependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter/artifactIdversion1.3.2/version
/dependency
添加 MySQL 和 MyBatis 支持。
2.2 配置数据库连接
设置application.properties文件添加如下配置
# MyBatis 配置
spring.datasource.urljdbc:mysql://172.16.10.79:3306/mytestdb?serverTimezoneUTCuseSSLfalseallowPublicKeyRetrievaltrue
spring.datasource.usernameroot
spring.datasource.password123456
spring.datasource.driver-class-namecom.mysql.cj.jdbc.Driver
mybatis.type-aliases-packagecom.hello.springboot.mapper
spring.datasource.url 数据库连接字符串spring.datasource.username 数据库用户名spring.datasource.password 数据库密码spring.datasource.driver-class-name 驱动类型注意MySQL 8.0的值是com.mysql.cj.jdbc.Driver和之前不同mybatis.type-aliases-package 配置mapper包名
Mapper文件说明
Mapper是MyBatis的核心是SQL存储的地方也是配置数据库映射的地方。
2.3 设置 MapperScan 包路径
直接在启动文件SpringbootApplication.java的类上配置MapperScan这样就可以省去单独给每个Mapper上标识Mapper的麻烦。
SpringBootApplication
MapperScan(com.hello.springboot.mapper)
public class SpringbootApplication {public static void main(String[] args) {SpringApplication.run(SpringbootApplication.class, args);}
}
2.4 添加代码
为了演示的简洁性我们不做太多的分层处理了我们这里就分为实体类、Mapper接口、Controller类使用Controller直接调用Mapper接口进行数据持久化处理。
User 类
public class User {private Long id;private String name;private int age;private String pwd;//省去set、get方法
}
UserMapper 接口
public interface UserMapper {Select(select * from user)Results({Result(property name, column name)})ListUser getAll();Select(select * from user where id#{id})User getById(Long id);Insert({insert into user(name,age,pwd) values(#{name},#{age},#{pwd})})void install(User user);Update({update user set name#{name} where id#{id}})void Update(User user);Delete(delete from user where id#{id})void delete(Long id);
}
可以看出来所有的SQL都是写在Mapper接口里面的。
Mapper里的注解说明
Select 查询注解Result 结果集标识用来对应数据库列名的如果实体类属性和数据库属性名保持一致可以忽略此参数Insert 插入注解Update 修改注解Delete 删除注解
Controller 控制器
RestController
RequestMapping(/)
public class UserController {Autowiredprivate UserMapper userMapper;RequestMapping(/)public ModelAndView index() {User user new User();user.setAge(18);user.setName(Adam);user.setPwd(123456);userMapper.install(user);ModelAndView modelAndView new ModelAndView(/index);modelAndView.addObject(count, userMapper.getAll().size());return modelAndView;}
}
到此为止已经完成了MyBatis项目的配置了可以运行调试了。
注解版GitHub源码下载https://github.com/vipstone/springboot-example/tree/master/springboot-mybatis
三、XML 版 MyBatis 集成
3.1 添加依赖
设置pom.xml文件添加如下配置
dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.12/version
/dependencydependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter/artifactIdversion1.3.2/version
/dependency
添加 MySQL 和 MyBatis 支持。
3.2 配置数据库连接
设置application.properties文件添加如下配置
# MyBatis 配置
spring.datasource.urljdbc:mysql://172.16.10.79:3306/mytestdb?serverTimezoneUTCuseSSLfalseallowPublicKeyRetrievaltrue
spring.datasource.usernameroot
spring.datasource.password123456
spring.datasource.driver-class-namecom.mysql.cj.jdbc.Driver
mybatis.type-aliases-packagecom.hello.springboot.entity
mybatis.config-locationsclasspath:mybatis/mybatis-config.xml
mybatis.mapper-locationsclasspath:mybatis/mapper/*.xml
spring.datasource.url 数据库连接字符串spring.datasource.username 数据库用户名spring.datasource.password 数据库密码spring.datasource.driver-class-name 驱动类型注意MySQL 8.0的值是com.mysql.cj.jdbc.Driver和之前不同mybatis.type-aliases-package 实体类包路径mybatis.config-locations 配置MyBatis基础属性mybatis.mapper-locations 配置Mapper XML文件
3.3 设置 MapperScan 包路径
直接在启动文件SpringbootApplication.java的类上配置MapperScan这样就可以省去单独给每个Mapper上标识Mapper的麻烦。
SpringBootApplication
MapperScan(com.hello.springboot.mapper)
public class SpringbootApplication {public static void main(String[] args) {SpringApplication.run(SpringbootApplication.class, args);}
}
3.4 配置XML文件
本示例设置两个xml文件在resource/mybatis下的mybatis-config.xml配置MyBatis基础属性和在resource/mybatis/mapper下的UserMapper.xml用户和数据交互的SQL语句。
mybatis-config.xml
?xml version1.0 encodingUTF-8 ?
!DOCTYPE configuration PUBLIC -//mybatis.org//DTD Config 3.0//EN http://mybatis.org/dtd/mybatis-3-config.dtdconfigurationtypeAliasestypeAlias aliasInteger typejava.lang.Integer/typeAlias aliasLong typejava.lang.Long/typeAlias aliasHashMap typejava.util.HashMap/typeAlias aliasLinkedHashMap typejava.util.LinkedHashMap/typeAlias aliasArrayList typejava.util.ArrayList/typeAlias aliasLinkedList typejava.util.LinkedList//typeAliases
/configuration
UserMapper.xml
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Config 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd!--namespace是命名空间是mapper接口的全路径--
mapper namespacecom.hello.springboot.mapper.UserMapper!--resultMap – 是最复杂也是最强大的元素用来描述如何从数据库结果集中来加载对象--resultMap iduserResultMap typecom.hello.springboot.entity.Userid propertyname columnusername/id/resultMap!--sql – 可被其他语句引用的可重用语句块--sql idcolumsid,username,age,pwd/sqlselect idfindAll resultMapuserResultMapselectinclude refidcolums /from user/selectselect idfindById resultMapuserResultMapselectinclude refidcolums /from userwhere id#{id}/selectinsert idinsert parameterTypecom.hello.springboot.entity.User INSERT INTOuser(username,age,pwd)VALUES(#{name}, #{age}, #{pwd})/insertupdate idupdate parameterTypecom.hello.springboot.entity.User UPDATEusersSETif testusername ! nullusername #{username},/ifif testpwd ! nullpwd #{pwd},/ifusername #{username}WHEREid #{id}/updatedelete iddelete parameterTypejava.lang.Long DELETE FROMuserWHEREid #{id}/delete/mapper
SQL 映射文件有很少的几个顶级元素按照它们应该被定义的顺序
cache – 给定命名空间的缓存配置。cache-ref – 其他命名空间缓存配置的引用。resultMap – 是最复杂也是最强大的元素用来描述如何从数据库结果集中来加载对象。parameterMap – 已废弃老式风格的参数映射。内联参数是首选,这个元素可能在将来被移除这里不会记录。sql – 可被其他语句引用的可重用语句块。insert – 映射插入语句update – 映射更新语句delete – 映射删除语句select – 映射查询语句
注意 MyBatis中 config 和 mapper 的 XML 头文件是不一样的。
config 头文件
?xml version1.0 encodingUTF-8 ?
!DOCTYPE configuration PUBLIC -//mybatis.org//DTD Config 3.0//EN http://mybatis.org/dtd/mybatis-3-config.dtd
mapper 头文件
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Config 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd
Mapper XML 更多配置http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html
3.5 添加代码
为了演示的便捷性我们添加3个类用于功能的展示分别是实体类User.java、mapper接口UserMapper.java和控制器类UserController.java使用控制器类直接调用UserMapper的方法进行数据存储和查询。
User.java
package com.hello.springboot.entity;
public class User {private Long id;private String name;private int age;private String pwd;//省略set/get方法
}UserMapper.java
package com.hello.springboot.mapper;
import com.hello.springboot.entity.User;
import java.util.List;
public interface UserMapper {ListUser findAll();User findById(Long id);void insert(User user);void update(User user);void delete(Long id);
}
注意 Mapper里的方法名必须和Mapper XML里的一致不然会找不到执行的SQL。
UserController.java
RestController
RequestMapping(/)
public class UserController {Resourceprivate UserMapper userMapper;RequestMapping(/)public ModelAndView index() {User user new User();user.setAge(18);user.setName(Adam);user.setPwd(123456);userMapper.insert(user);ModelAndView modelAndView new ModelAndView(/index);modelAndView.addObject(count, userMapper.findAll().size());return modelAndView;}}
到此为止已经完成了MyBatis项目的配置了可以运行调试了。
XML版GitHub源码下载https://github.com/vipstone/springboot-example/tree/master/springboot-mybatis-xml
四、总结
到目前为止我们已经掌握了MyBatis的两种集成方式注解集成和XML集成注解版更符合程序员的代码书写习惯适用于简单快速查询XML版可以灵活的动态调整SQL更适合大型项目开发具体的选择还要看开发场景以及个人喜好了。