目录
- 一、JDBC和MyBatis的简介和比较
- 1)MyBatis介绍
- 2)JDBC问题总结如下
- 3)Mybatis解决jdbc编程的问题
- 二、创建项目和配置
- 1)创建SpringBoot项目
- 2)创建包和文件(以及文件代码)
- 三、数据库表的创建
- 四、结果
- 五、总结
- 六、参考链接
一、JDBC和MyBatis的简介和比较
1)MyBatis介绍
MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis,实质上Mybatis对ibatis进行一些改进。
MyBatis是一个优秀的持久层框架,它对jdbc的操作数据库的过程进行封装,使开发者只需要关注 SQL 本身,而不需要花费精力去处理例如注册驱动、创建connection、创建statement、手动设置参数、结果集检索等jdbc繁杂的过程代码。
Mybatis通过xml或注解的方式将要执行的各种statement(statement、preparedStatemnt、CallableStatement)配置起来,并通过java对象和statement中的sql进行映射生成最终执行的sql语句,最后由mybatis框架执行sql并将结果映射成java对象并返回。
2)JDBC问题总结如下
1、 数据库链接创建、释放频繁造成系统资源浪费从而影响系统性能,如果使用数据库链接池可解决此问题。
2、 Sql语句在代码中硬编码,造成代码不易维护,实际应用sql变化的可能较大,sql变动需要改变Java代码。
3、 使用preparedStatement向占有位符号传参数存在硬编码,因为sql语句的where条件不一定,可能多也可能少,修改sql还要修改代码,系统不易维护。
4、 对结果集解析存在硬编码(查询列名),sql变化导致解析代码变化,系统不易维护,如果能将数据库记录封装成pojo对象解析比较方便。
3)Mybatis解决jdbc编程的问题
1、 数据库链接创建、释放频繁造成系统资源浪费从而影响系统性能,如果使用数据库链接池可解决此问题。
解决:在SqlMapConfig.xml中配置数据链接池,使用连接池管理数据库链接。
2、 Sql语句写在代码中造成代码不易维护,实际应用sql变化的可能较大,sql变动需要改变java代码。
解决:将Sql语句配置在XXXXmapper.xml文件中与java代码分离。
3、 向sql语句传参数麻烦,因为sql语句的where条件不一定,可能多也可能少,占位符需要和参数一一对应。
解决:Mybatis自动将java对象映射至sql语句,通过statement中的parameterType定义输入参数的类型。
4、 对结果集解析麻烦,sql变化导致解析代码变化,且解析前需要遍历,如果能将数据库记录封装成pojo对象解析比较方便。
解决:Mybatis自动将sql执行结果映射至java对象,通过statement中的resultType定义输出结果的类型。
二、创建项目和配置
1)创建SpringBoot项目
1.打开IDEA,file–>new–>project:
2.选择spring initializr,再next:
3.自主命名,选择java versions为8,再next:
4.点击web,选择spring web:
5.点击SQL,选择三个(如图),再next:
6.输入文件名,选择地址,点击Finnish就创建完成了:
2)创建包和文件(以及文件代码)
1.完整项目可以在我的github上面获取:获取地址
2.创建文件
①在src–>main–>java–>com.mybitis(自动创建的)下,创建四个包分别为:controller、entity、service、mapper,又在包下创建四个java文件:UserController、User、UserMapper(该文件为接口)、UserService。
UserController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49package com.mybitis.controller; import com.mybitis.entity.User; import com.mybitis.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @RequestMapping("/getAllUser") public List<User> findAll(){ return userService.findAllUser(); } @RequestMapping("/getUserByUserID/{userid}") public List<User> findUserByUserId(@PathVariable int userid){ return userService.findUserByUserId(userid); } @RequestMapping("/getUserByUsername/{username}") public List<User> findUserByUsername(@PathVariable String username){ return userService.findUserByUsername(username); } @RequestMapping("/insertUser") public User insertUser(User user){ return userService.insertUser(user); } @RequestMapping("/updateUser") public int updateUser(User user){ return userService.updateUser(user); } @RequestMapping("/deleteUser") public int deleteUser(User user){ return userService.deleteUser(user); } }
User.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44package com.mybitis.entity; public class User { private int userid; private String username; private String password; public int getUserid() { return userid; } public void setUserid(int userid) { this.userid = userid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" + "userid=" + userid + ", username='" + username + ''' + ", password='" + password + ''' + '}'; } }
UserService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41package com.mybitis.service; import com.mybitis.entity.User; import com.mybitis.mapper.UserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserService { @Autowired(required=false) public UserMapper userMapper; public List<User> findAllUser(){ return userMapper.findAllUser(); } public List<User> findUserByUserId(int userid){ return userMapper.findUserByUserId(userid); } public List<User> findUserByUsername(String username){ return userMapper.findUserByUsername(username); } public User insertUser(User user){ userMapper.insertUser(user); return user; } public int updateUser(User user){ return userMapper.updateUser(user); } public int deleteUser(User user){ return userMapper.deleteUser(user); } }
UserMapper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15package com.mybitis.mapper; import com.mybitis.entity.User; import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public interface UserMapper { public List<User> findAllUser(); public List<User> findUserByUserId(int userid); public List<User> findUserByUsername(String username); public int insertUser(User user); public int updateUser(User user); public int deleteUser(User user); }
②在resources里建一个mapper包,包下建一个与上面同名的一个UserMapper的xml文件。
UserMapper.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.mybitis.mapper.UserMapper"> <resultMap id="result" type="com.mybitis.entity.User"> <result column="userid" jdbcType="INTEGER" property="userid" /> <result column="username" jdbcType="VARCHAR" property="username" /> <result column="password" jdbcType="VARCHAR" property="password" /> </resultMap> <select id="findAllUser" resultType="com.mybitis.entity.User"> select * from user; </select> <select id="findUserByUserId" resultType="com.mybitis.entity.User"> select * from user where userid=#{userid}; </select> <select id="findUserByUsername" resultType="com.mybitis.entity.User"> select * from user where username=#{username}; </select> <insert id="insertUser" parameterType="com.mybitis.entity.User" keyProperty="userid" useGeneratedKeys="true"> insert into user(userid,username,password) values (#{userid},#{username},#{password}); </insert> <update id="updateUser" parameterType="com.mybitis.entity.User"> update user set username=#{username},password=#{password} where userid=#{userid}; </update> <delete id="deleteUser" parameterType="com.mybitis.entity.User"> delete from user where userid=#{userid}; </delete> </mapper>
③在resources里有一个application.properties文件,将该文件删除,在创建两个文件:application.yml、application-dev.yml。
application.yml
1
2
3
4spring: profiles: active: dev
application-dev.yml
1
2
3
4
5
6
7
8
9
10
11server: port: 8080 #端口号 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false #3306/后面是数据库的名字 username: root #账户 password: xhy200104274 #数据库密码 mybatis: mapper-locations: classpath:mapper/*Mapper.xml
总结构如下图:
三、数据库表的创建
使用navicat:
表中内容:
四、结果
1.使用的postman测试:
2.通过id查名字:
3.插入:
4.更新:
5.删除:
五、总结
这次实验还是简单的,就是要将配置弄好,MyBatis与JDBC还是有很大差别,将SQL的语句添加到了配置文件里,将Java和SQL语句分开,在SQL语句多的时候便于管理和书写。
六、参考链接
1.【蠢事】Spring Boot项目启动访问页面报错Initializing Spring DispatcherServlet ‘dispatcherServlet’
2.springboot-服务启动后访问报错Initializing Spring DispatcherServlet ‘dispatcherServlet‘
3.异常:This application has no explicit mapping for /error, so you are seeing this as a fallback解决方法
4.SpringBoot启动报错:HikariPool-1 - Exception during pool initialization.
5.IDEA2019开发Spring Boot整合Mybatis实现User的CRUD(增读更删)
6.Mybatis与JDBC的对比超详细笔记
最后
以上就是俊逸汉堡最近收集整理的关于使用MyBatis连接数据库一、JDBC和MyBatis的简介和比较二、创建项目和配置三、数据库表的创建四、结果五、总结六、参考链接的全部内容,更多相关使用MyBatis连接数据库一、JDBC和MyBatis内容请搜索靠谱客的其他文章。
发表评论 取消回复