1.我用的开发工具是Spring-tool-Suite。这个工具适合创建Spting类型的项目。(这篇文章开头有使用介绍:http://blog.csdn.net/baidu_36216018/article/details/79461016;使用IDEA工具也行,)
2.创建一个项目:
① new--->Spring Stater project
②
③:搜索需要的依赖包
④:我们发现上一步选择的依赖,在pom.xml文件中 已经自动添加到我们的文件中了。
2. 开始编写代码和一些配置
先看一下我的项目的最后目录结构:
①:自己创建一个数据表 字段为id , name, password ,number 四个字段 id为int类型,其他的都为String类型。
在src/main/java目录下添加controller层,entity层,mapper层,service层
entity层的实体类User:
package com.example.demo.entity;
public class User {
private int id;
private String name;
private String password;
private String number;
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 getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", password=" + password + ", number=" + number + "]";
}
}
mapper层的UserMapper类:
package com.example.demo.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.example.demo.entity.User;
@Mapper
public interface UserMapper {
List findUserByName(String name);
public List ListUser();
public int insertUser(User user);
public int delete(int id);
public int Update(User user);
}
service层的实现类Userservice:
package com.example.demo.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public List findByName(String name) {
return userMapper.findUserByName(name);
}
public User insertUser(User user) {
userMapper.insertUser(user);
return user;
}
public List ListUser(){
return userMapper.ListUser();
}
public int Update(User user){
return userMapper.Update(user);
}
public int delete(int id){
return userMapper.delete(id);
}
}
controller层 的访问类CRUD:
package com.example.demo.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
@RestController
@RequestMapping(value = "/CRUD", method = { RequestMethod.GET, RequestMethod.POST })
public class CRUD {
@Autowired
private UserService userservice;
@RequestMapping(value = "/delete", method = RequestMethod.GET)
public String delete(int id) {
int result = userservice.delete(id);
if (result >= 1) {
return "删除成功";
} else {
return "删除失败";
}
}
@RequestMapping(value = "/update", method = RequestMethod.POST)
public String update(User user) {
int result = userservice.Update(user);
if (result >= 1) {
return "修改成功";
} else {
return "修改失败";
}
}
@RequestMapping(value = "/insert", method = RequestMethod.POST)
public User insert(User user) {
return userservice.insertUser(user);
}
@RequestMapping("/ListUser")
@ResponseBody
public List ListUser(){
return userservice.ListUser();
}
@RequestMapping("/ListUserByname")
@ResponseBody
public List ListUserByname(String name){
return userservice.findByName(name);
}
}
接着:
在src/main/resources下写UserMapper的映射文件xml.
UserMapper.xml
INSERT INTO user
(
id,name,password,number
)
VALUES (
#{id},
#{name, jdbcType=VARCHAR},
#{password, jdbcType=VARCHAR},
#{number}
)
delete from user where id=#{id}
update user set user.name=#{name},user.password=#{password},user.number=#{number} where user.id=#{id}
最后配置application.properties配置文件:
spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = 11111111
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
mybatis.mapper-locations= classpath:mapper/*.xml
启动程序的入口类:SpringbootMybatisApplication.java
我们在postmain这个工具中测试:
测试查询所有的信息:http://localhost:8080/CRUD/ListUser
测试根据name查询数据:
http://localhost:8080/CRUD/ListUserByname?name=小王
测试根据Id删除数据:
http://localhost:8080/CRUD/delete?id=40
测试根据插入数据:http://localhost:8080/CRUD/insert?name=波波&password=123456&number=123
测试修改数据:http://localhost:8080/CRUD/update?id=46&name=zyb&password=9999999&number=5555
结束!源码地址-->码云:源码地址