1. 新建SpringBoot项目
这块就不过多赘述了,就简单的方法例如使用idea初始化一个springBoot项目
2. maven依赖配置(pom.xml)
版本号根据实际使用springBoot版本做适当调整
2.1 如果是一个web项目,需要有以下配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2.2 引入MyBatis依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
2.3 引入数据库JDBC依赖包
选择适用的jdbc依赖包
2.3.1 mysql jdbc 依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
2.3.2 sqlSer jdbc 依赖
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
</dependency>
3. 配置数据库连接
配置项目resource/application.properties文件(没有的话新建一个)
3.1 mysql配置
# web服务端口号
server.port=8081
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.driver-class-name=org.gjt.mm.mysql.Driver
spring.datasource.username=root
spring.datasource.password=123456
mybatis.mapper-locations=classpath*:mapper/*.xml
3.2 sqlServer配置
# web服务端口号
server.port=8081
spring.datasource.url=jdbc:mysql://localhost:1433/test
spring.datasource.driver-class-name=net.sourceforge.jtds.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456
mybatis.mapper-locations=classpath*:mapper/*.xml
4. 创建 表 和 实体映射类 Entity
建表
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
insert into user(name,age) values('aaron',18);
实体映射类
package com.example.entity;
/**
* (User)实体类
*/
public class User {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
5. 新建实体类对应的mapper配置文件
resource/mapper/UserDao.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.example.dao.UserDao">
<resultMap type="com.example.entity.User" id="UserMap">
<result property="id" column="id" jdbcType="INTEGER"/>
<result property="name" column="name" jdbcType="VARCHAR"/>
<result property="age" column="age" jdbcType="INTEGER"/>
</resultMap>
<!--查询单个-->
<select id="queryById" resultMap="UserMap">
select
id, name, age
from test.user
where id = #{id}
</select>
<!--查询指定行数据-->
<select id="queryAllByLimit" resultMap="UserMap">
select
id, name, age
from test.user
limit #{offset}, #{limit}
</select>
<!--通过实体作为筛选条件查询-->
<select id="queryAll" resultMap="UserMap">
select
id, name, age
from test.user
<where>
<if test="id != null">
and id = #{id}
</if>
<if test="name != null and name != ''">
and name = #{name}
</if>
<if test="age != null">
and age = #{age}
</if>
</where>
</select>
<!--新增所有列-->
<insert id="insert" keyProperty="id" useGeneratedKeys="true">
insert into test.user(name, age)
values (#{name}, #{age})
</insert>
<!--通过主键修改数据-->
<update id="update">
update test.user
<set>
<if test="name != null and name != ''">
name = #{name},
</if>
<if test="age != null">
age = #{age},
</if>
</set>
where id = #{id}
</update>
<!--通过主键删除-->
<delete id="deleteById">
delete from test.user where id = #{id}
</delete>
</mapper>
6. 编写对应的service、controller代码
6.1 controller层
package com.example.controller;
import com.example.entity.User;
import com.example.service.UserService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/**
* (User)表控制层
*/
@RestController
@RequestMapping("user")
public class UserController {
/**
* 服务对象
*/
@Resource
private UserService userService;
/**
* 通过主键查询单条数据
*
* @param id 主键
* @return 单条数据
*/
@GetMapping("selectOne")
public User selectOne(Integer id) {
return this.userService.queryById(id);
}
@PostMapping("addOne")
public User addOne(@RequestBody User user) {
return this.userService.insert(user);
}
}
6.2 service层
package com.example.service;
import com.example.entity.User;
import java.util.List;
/**
* (User)表服务接口
*
* @author makejava
* @since 2020-03-07 13:27:58
*/
public interface UserService {
/**
* 通过ID查询单条数据
*
* @param id 主键
* @return 实例对象
*/
User queryById(Integer id);
/**
* 查询多条数据
*
* @param offset 查询起始位置
* @param limit 查询条数
* @return 对象列表
*/
List<User> queryAllByLimit(int offset, int limit);
/**
* 新增数据
*
* @param user 实例对象
* @return 实例对象
*/
User insert(User user);
/**
* 修改数据
*
* @param user 实例对象
* @return 实例对象
*/
User update(User user);
/**
* 通过主键删除数据
*
* @param id 主键
* @return 是否成功
*/
boolean deleteById(Integer id);
}
package com.example.service.impl;
import com.example.entity.User;
import com.example.dao.UserDao;
import com.example.service.UserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* (User)表服务实现类
*
* @author makejava
* @since 2020-03-07 13:27:58
*/
@Service("userService")
public class UserServiceImpl implements UserService {
@Resource
private UserDao userDao;
/**
* 通过ID查询单条数据
*
* @param id 主键
* @return 实例对象
*/
@Override
public User queryById(Integer id) {
return this.userDao.queryById(id);
}
/**
* 查询多条数据
*
* @param offset 查询起始位置
* @param limit 查询条数
* @return 对象列表
*/
@Override
public List<User> queryAllByLimit(int offset, int limit) {
return this.userDao.queryAllByLimit(offset, limit);
}
/**
* 新增数据
*
* @param user 实例对象
* @return 实例对象
*/
@Override
public User insert(User user) {
this.userDao.insert(user);
return user;
}
/**
* 修改数据
*
* @param user 实例对象
* @return 实例对象
*/
@Override
public User update(User user) {
this.userDao.update(user);
return this.queryById(user.getId());
}
/**
* 通过主键删除数据
*
* @param id 主键
* @return 是否成功
*/
@Override
public boolean deleteById(Integer id) {
return this.userDao.deleteById(id) > 0;
}
}
7. 运行程序,访问接口
启动程序
运行程序,访问对应端口服务
浏览器打开
http://localhost:8081/user/selectOne?id=1
done