以下是“Java+MyBatis+MySQL开发环境搭建流程详解”的攻略。
准备工作
- 安装JDK及配置环境变量
- 安装MySQL数据库及客户端
- 安装MyBatis框架及依赖库
创建数据库及表
- 创建数据库
在MySQL客户端中执行以下SQL语句,创建一个名为testdb
的数据库:
CREATE DATABASE testdb;
- 创建表
继续在MySQL客户端中执行以下SQL语句,创建一个名为user
的表:
CREATE TABLE user (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
age INT(11) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
配置MyBatis
- 配置数据库连接信息
在src/main/resources
目录下创建jdbc.properties
文件,配置数据库连接信息:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/testdb?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=
- 配置MyBatis映射文件
在src/main/resources
目录下创建UserMapper.xml
文件,配置MyBatis的映射文件:
<?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.example.mapper.UserMapper">
<resultMap type="com.example.entity.User" id="UserResultMap">
<id property="id" column="id" />
<result property="name" column="name" />
<result property="age" column="age" />
</resultMap>
<select id="getUserById" resultMap="UserResultMap">
SELECT * FROM user WHERE id=#{id}
</select>
<select id="getUserList" resultMap="UserResultMap">
SELECT * FROM user
</select>
<insert id="insertUser">
INSERT INTO user(name, age) VALUES(#{name}, #{age})
</insert>
<update id="updateUser">
UPDATE user SET name=#{name}, age=#{age} WHERE id=#{id}
</update>
<delete id="deleteUser">
DELETE FROM user WHERE id=#{id}
</delete>
</mapper>
编写Java代码
- 创建实体类
在src/main/java
目录下创建com.example.entity.User
类,表示用户实体:
package com.example.entity;
public class User {
private Integer id;
private String name;
private Integer age;
// getter and setter
}
- 创建Mapper接口
在src/main/java
目录下创建com.example.mapper.UserMapper
接口,定义MyBatis的操作方法:
package com.example.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.example.entity.User;
@Mapper
public interface UserMapper {
User getUserById(Integer id);
List<User> getUserList();
int insertUser(User user);
int updateUser(User user);
int deleteUser(Integer id);
}
- 编写DAO层
在src/main/java
目录下创建com.example.dao.UserDao
类,调用MyBatis框架的Mapper接口实现DAO操作:
package com.example.dao;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.example.entity.User;
import com.example.mapper.UserMapper;
@Repository
public class UserDao {
@Autowired
private UserMapper userMapper;
public User getUserById(Integer id) {
return userMapper.getUserById(id);
}
public List<User> getUserList() {
return userMapper.getUserList();
}
public int insertUser(User user) {
return userMapper.insertUser(user);
}
public int updateUser(User user) {
return userMapper.updateUser(user);
}
public int deleteUser(Integer id) {
return userMapper.deleteUser(id);
}
}
运行示例
- 编写Controller
在src/main/java
目录下创建com.example.controller.UserController
类,处理HTTP请求:
package com.example.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.dao.UserDao;
import com.example.entity.User;
@RestController
public class UserController {
@Autowired
private UserDao userDao;
@GetMapping("/user/{id}")
public User getUserById(@PathVariable("id") Integer id) {
return userDao.getUserById(id);
}
@GetMapping("/user/list")
public List<User> getUserList() {
return userDao.getUserList();
}
@PostMapping("/user")
public String insertUser(@RequestBody User user) {
userDao.insertUser(user);
return "success";
}
@PostMapping("/user/{id}")
public String updateUser(@PathVariable("id") Integer id, @RequestBody User user) {
user.setId(id);
userDao.updateUser(user);
return "success";
}
@PostMapping("/user/delete/{id}")
public String deleteUser(@PathVariable("id") Integer id) {
userDao.deleteUser(id);
return "success";
}
}
- 启动应用
在命令行中执行mvn spring-boot:run
,启动应用。
- 测试API
使用浏览器或Postman工具,访问以下API:
方法 | API | 请求体 | 返回值 |
---|---|---|---|
GET | http://localhost:8080/user/1 | {"id":1,"name":"张三","age":20} | |
GET | http://localhost:8080/user/list | [{"id":1,"name":"张三","age":20},...] | |
POST | http://localhost:8080/user | {"name":"李四","age":25} | "success" |
POST | http://localhost:8080/user/1 | {"name":"王五","age":30} | "success" |
POST | http://localhost:8080/user/delete/1 | "success" |
其中,前两个API用于查询用户,第三个API用于新增用户,第四个API用于修改用户信息,第五个API用于删除用户。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Java+MyBatis+MySQL开发环境搭建流程详解 - Python技术站