主要是更换了下sqlite的数据源而已,其他代码不变。
我都贴一下吧,这个算是比较通用的基础增删改查代码。
可以使用Idea自带的Database插件配置,也可以命令行创建,具体我就不讲了,看下图
初始化数据:
create table tb_user
(
id integer primary key autoincrement not null,
username varchar(20),
sex varchar(10),
age integer
)
;
insert into tb_user(username,sex,age) values ('Jack','male',20);
insert into tb_user(username,sex,age) values ('Lucy','female',21);
insert into tb_user(username,sex,age) values ('Dylan','male',18);
spring:
datasource:
driver-class-name: org.sqlite.JDBC
url: jdbc:sqlite:D:/ideaprojects/sqlitedemo/src/main/resources/test.db
username:
password:
#mybatis
mybatis:
mapperLocations: classpath*:mapper/*.xml
package com.laoxu.test.sqlitedemo.model;
import lombok.Data;
/**
* @Description:
* @Author laoxu
* @Date 2019/11/5 22:25
**/
@Data
public class User {
private Integer id;
private String username;
private String sex;
private Integer age;
}
package com.laoxu.test.sqlitedemo.mapper;
import com.laoxu.test.sqlitedemo.model.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @Description:
* @Author laoxu
* @Date 2019/11/5 22:27
**/
@Mapper
@Component
public interface UserDao {
int insert(User user);
User select(Integer id);
List selectAll();
int delete(Integer id);
int update(User user);
}
insert into tb_user (
username,
sex,
age
)
VALUES (
#{username},
#{sex},
#{age}
)
update tb_user
set username=#{username},
sex=#{sex},
age = #{age}
where id=#{id}
DELETE FROM tb_user
WHERE id = #{id}
package com.laoxu.test.sqlitedemo.service;
import com.laoxu.test.sqlitedemo.mapper.UserDao;
import com.laoxu.test.sqlitedemo.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.jws.soap.SOAPBinding;
import java.util.List;
/**
* @Description:
* @Author laoxu
* @Date 2019/11/5 23:10
**/
@Service
public class UserService {
@Autowired
private UserDao userDao;
public boolean add(User user) {
return userDao.insert(user) > 0;
}
public User getOne(int id) {
return userDao.select(id);
}
public List getAll() {
return userDao.selectAll();
}
public boolean modify(User user) {
return userDao.update(user) > 0;
}
public boolean remove(Integer id) {
return userDao.delete(id) > 0;
}
}
package com.laoxu.test.sqlitedemo.controller;
import com.laoxu.test.sqlitedemo.model.User;
import com.laoxu.test.sqlitedemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* @Description:
* @Author laoxu
* @Date 2019/11/5 23:13
**/
@RestController
@RequestMapping("/api/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/get/{id}")
public User get(@PathVariable Integer id){
return userService.getOne(id);
}
@GetMapping("/getAll")
public List getAll(){
return userService.getAll();
}
@PostMapping("/remove")
public Boolean remove(@RequestParam Integer id){
return userService.remove(id);
}
@PostMapping("/add")
public Boolean add(@RequestBody User user){
return userService.add(user);
}
@PostMapping("/modify")
public Boolean update(@RequestBody User user){
return userService.modify(user);
}
}
---------------------
----------------------
-----------------------
需要源码的朋友可以留下邮箱。