- 一、MyBatis
- 1.1 MyBatis 介绍
- 1.2 创建项目
- 1.3 配置项目并编写代码
- 1.4 测试
- 二、总结
MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Ordinary Java Object,普通的 Java对象)映射成数据库中的记录。MyBatis 是支持普通 SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的POJOs(Plain Ordinary Java Objects,普通的 Java对象)映射成数据库中的记录。
1.2 创建项目1.选择Spring Initializr
2.选择Java 版本,其他的可不做更改,直接点击next
3.选择项目依赖,点击next
4.给项目命名,点击Finish
server.port=8080 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/runoob?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=123456 mybatis.mapper-locations=classpath:mapper/*Mapper.xml
在com.example.demo下创建四个package(controller、entity、mapper、service):
在entity 下创建类Websites:
Websites :`
package com.example.demo.entity;
public class Websites {
private int id;
private String name;
private String url;
private int alexa;
private String country;
public int getId() {return id;}
public void setId(int id) {this.id = id;}
public String getName() {return name;}
public void setName(String name) {this.name = name;}
public String getUrl() {return url;}
public void setUrl(String url) {this.url = url;}
public int getAlexa() {return alexa;}
public void setAlexa(int alexa) {this.alexa = alexa;}
public String getCountry() {return country;}
public void setCountry(String country) {this.country = country;}
@Override
public String toString(){
return "Websites{" +
"id=" + id +
", name='" + name +
", url=" + url +
", alexa=" + alexa +
", country=" + country +
"}";
}
}
在mapper 下创建接口WebMapper
WebMapper :
package com.example.demo.mapper;
import com.example.demo.entity.Websites;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
public interface WebMapper {
@Mapper
public List findAllWebsites();
public List findWebsitesById(int id);
}
在resources下创建目录mapper,并创建WebMapper.xml文件:
WebMapper.xml :
select * from websites;
在包Service 下创建类WebService:
WebService :
package com.example.demo.service;
import com.example.demo.entity.Websites;
import com.example.demo.mapper.WebMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class WebService {
@Autowired(required = false)
public WebMapper webMapper;
public List findAllWebsites(){
return webMapper.findAllWebsites();
}
public List findWebsitesById(int id){
return webMapper.findWebsitesById(id);
}
}
在包controller下创建类WebController :
package com.example.demo.controller;
import com.example.demo.entity.Websites;
import com.example.demo.service.WebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/websites")
public class WebController {
@Autowired
private WebService webService;
@RequestMapping("/getAllWebsites")
public List findAllWebsites(){
return webService.findAllWebsites();
}
@RequestMapping("/getWebsitesById")
public List findWebsitesById(int id){
return webService.findWebsitesById(id);
}
}
扫包,在spring主程序上面添加这个注解
@MapperScan(basePackages = “com.xxx.mapper”)
运行项目
在浏览器输入:http://localhost:8080/websites/getAllWebsites进行测试
1). JDBC:
数据库链接创建、释放频繁造成系统资源浪费从而影响系统性能,Sql语句在代码中硬编码,造成代码不易维护,实际应用中sql变化的可能较大,sql变动需要改变java代码。 使用preparedStatement向占有位符号传参数存在硬编码,因为sql语句的where条件不一定,可能多也可能少,修改sql还要修改代码,系统不易维护。对结果集解析存在硬编码(查询列名),sql变化导致解析代码变化,系统不易维护,如果能将数据库记录封装成pojo对象解析比较方便。
2)Mybatis
优化获取和释放, SQL统一管理,对数据库进行存取操作(修改SQL一个需要重新编译 一个不需要)。生成动态SQL语句。 能够对结果集进行映射(一个是resultset一个可以映射成JavaBean对象、一个Map、一个List等等)。mybatis提供了一级和二级缓存(需要配置打开),强大的动态sql,自动化的session管理,都比手工维护来的方便和安全。



