这个网络上已经有很多实例了,写这个也是想着按照自己的工作的方式来进行连接,仅供大家参考。
1、打开idea,选择file-new project,选择spring Initializr,如下图:
2、点击下一步,修改项目名称,然后选择jar包的版本,我习惯选择8版本
3、点击下一步,选择对应的插件,如下图:
4、点击完成后,初步创建项目完成。然后创建数据库和对应的表。表可以按照自己需求来创建,如下图:
5、创建好数据库和表以后,可以在idea的application.properties上配置数据库连接的设置,如下图:
server.port=8082
mybatis.mapper-locations=classpath:mapper/*.xml
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
spring.datasource.username=root
spring.datasource.password=123root
6、在entity路径下,创建实体类Area,实体类字段与表字段相对应,如下图:
package com.gzb.springbootmybatis.entity;
import java.util.Date;
/**
* @author: Author
* @Date:2023/10/7 17:02
* @Description:
* @Version: 1.0
*/
public class Area {
private Integer areaId;
private String areaName;
private Integer priority;
private Date createTime;
private Date lastEditTime;
public Integer getAreaId() {
return areaId;
}
public void setAreaId(Integer areaId) {
this.areaId = areaId;
}
public String getAreaName() {
return areaName;
}
public void setAreaName(String areaName) {
this.areaName = areaName;
}
public Integer getPriority() {
return priority;
}
public void setPriority(Integer priority) {
this.priority = priority;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getLastEditTime() {
return lastEditTime;
}
public void setLastEditTime(Date lastEditTime) {
this.lastEditTime = lastEditTime;
}
@Override
public String toString() {
return "Area{" +
"areaId=" + areaId +
", areaName='" + areaName + '\'' +
", priority=" + priority +
", createTime=" + createTime +
", lastEditTime=" + lastEditTime +
'}';
}
}
7、controller下添加访问类,如下图:
package com.gzb.springbootmybatis.controller;
import com.gzb.springbootmybatis.entity.Area;
import com.gzb.springbootmybatis.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author: Author
* @Date:2023/10/7 16:58
* @Description:
* @Version: 1.0
*/
@RestController
@RequestMapping("/area")
public class AreaController {
@Autowired
private AreaService areaService;
@RequestMapping("/getAllArea.do")
public List<Area> getAllArea(){
return areaService.getAllArea();
}
@RequestMapping("/inserArea.do")
public int inserAreaInfo(@RequestBody Area area){
return areaService.inserArea(area);
}
@RequestMapping("/deleteArea.do")
public void deleteArea(@RequestBody Area area){
areaService.deleteAreaById(area);
}
}
8、dao添加数据库操作类
package com.gzb.springbootmybatis.dao;
import com.gzb.springbootmybatis.entity.Area;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* @author: Author
* @Date:2023/10/7 17:07
* @Description:
* @Version: 1.0
*/
@Mapper
public interface AreaMapper {
public List<Area> getAllArea();
// 插入数据
public int insertArea(Area area);
// 根据id删除区域
public void deleteAreaById(Area area);
}
9、service下添加逻辑代码,如下图:
package com.gzb.springbootmybatis.service;
import com.gzb.springbootmybatis.dao.AreaMapper;
import com.gzb.springbootmybatis.entity.Area;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author: Author
* @Date:2023/10/7 17:00
* @Description:
* @Version: 1.0
*/
@Service
public class AreaService {
@Autowired(required = false)
private AreaMapper areaMapper;
public List<Area> getAllArea() {
return areaMapper.getAllArea();
}
public int inserArea(Area area) {
int num = 0;
areaMapper.insertArea(area);
return num;
}
public void deleteAreaById(Area area) {
areaMapper.deleteAreaById(area);
}
}
10、resource文件夹下添加mapper文件夹,然后mapper文件夹添加数据库操作语言,也就是sql
AreaMapper.xml内容如下:
<?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.gzb.springbootmybatis.dao.AreaMapper">
<resultMap id="result" type="com.gzb.springbootmybatis.entity.Area">
<result column="area_id" jdbcType="INTEGER" property="areaId"/>
<result column="area_name" jdbcType="VARCHAR" property="areaName"/>
<result column="priority" jdbcType="INTEGER" property="priority"/>
<result column="create_time" jdbcType="DATE" property="createTime"/>
<result column="last_edit_time" jdbcType="DATE" property="lastEditTime"/>
</resultMap>
<select id="getAllArea" resultMap="result" resultType="com.gzb.springbootmybatis.entity.Area">
select * from tb_area
order by priority desc ;
</select>
<insert id="insertArea" parameterType="com.gzb.springbootmybatis.entity.Area" useGeneratedKeys="true" keyProperty="areaId">
insert into tb_area
(
area_name,
priority,
create_time,
last_edit_time)
values (
#{areaName},
#{priority},
#{createTime},
#{lastEditTime}
)
</insert>
<delete id="deleteAreaById" parameterType="com.gzb.springbootmybatis.entity.Area" flushCache="true">
delete from tb_area
where area_id = #{areaId}
</delete>
</mapper>
其中xml内的id需要与AreaMapper.java里面的函数名一致,parameterType则是传参的类型。
10、完成上面这几步之后,先检查有没有错误,如果没有错误的话,则可以开始启动服务器,进行测试了。如下图:
以上就是使用idea+mybatis连接mysql的过程,写得比较乱,如果还有不太明白的地方,可以一起讨论和改进。