一、引入依赖
SpringBoot版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
在pom.xml引入mybatis-spring-boot-starter的依赖:
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
引入数据库连接依赖:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
二、引入数据源
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/dbtest?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=***
三、实体类
package com.yms.demo.domain.pojo;
public class Test {
private Integer id;
private String username;
private String password;
private String time;
get...set...
}
import java.io.Serializable;
public class Result implements Serializable {
private boolean isSuccess;
private String message;
public Result(boolean isSuccess, String message) {
this.isSuccess = isSuccess;
this.message = message;
}
get...set...
}
工具类:
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateHelpler {
private static String datePattern = "yyyy-MM-dd";
private static String timePattern = "yyyy-MM-dd HH:mm";
private static String dateMoth = "yyyy-MM";
private static String timePattern2 = "yyyy-MM-dd HH:mm:ss";
private static String timeForAlarm = "yyyy.MM.dd HH:mm:ss";
private static String timePattern3 = "yyyy年MM月dd日HH时";
public static String getDateNow() {
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(timePattern2);
String timeStampStr = simpleDateFormat.format(date);
return timeStampStr;
}
}
四、dao层
package com.yms.demo.dao;
import com.yms.demo.domain.pojo.Test;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface TestMapper {
@Insert("insert into test (username,password,time)values(#{username}, #{password}, #{time})")
Integer add(@Param("username") String username, @Param("password") String passwrd, @Param("time") String time);
@Update("update test set password = #{password}, time = #{time} where username = #{username}")
Integer update(@Param("username") String username, @Param("password") String password, @Param("time") String time);
@Delete("delete from test where id = #{id}")
Integer delete(@Param("id") Integer id);
@Select("select * from test where id = #{id}")
Test findOne(@Param("id") Integer id);
@Select("select * from test")
List<Test> findAll();
}
五、service层
package com.yms.demo.service;
import com.yms.demo.common.utils.DateHelpler;
import com.yms.demo.dao.TestMapper;
import com.yms.demo.domain.pojo.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class TestService{
@Autowired
private TestMapper testMapper;
public Integer add(String username, String passwrd) {
String time = DateHelpler.getDateNow();
return testMapper.add(username,passwrd,time);
}
public Integer update(String username, String password) {
String time = DateHelpler.getDateNow();
return testMapper.update(username,password,time);
}
public Integer delete(Integer id) {
return testMapper.delete(id);
}
public Test findOne(Integer id) {
return testMapper.findOne(id);
}
public List<Test> findAll() {
return testMapper.findAll();
}
}
六、Webcontroller层
package com.yms.demo.web;
import com.yms.demo.domain.entity.Result;
import com.yms.demo.domain.pojo.Test;
import com.yms.demo.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
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;
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
private TestService testService;
@RequestMapping("/findAll")
public List<Test> findAll() {
return testService.findAll();
}
@RequestMapping("/findOne")
public Test findOne(Integer id) {
return testService.findOne(id);
}
@PostMapping("/add")
public Result add(@RequestBody Test test) {
try {
testService.add(test.getUsername(),test.getPassword());
return new Result(true,"add成功");
} catch (Exception e) {
return new Result(false,"add失败");
}
}
@RequestMapping("/delete")
public Result delete(Integer id) {
try {
testService.delete(id);
return new Result(true,"delete成功");
} catch (Exception e) {
return new Result(false,"delete失败");
}
}
@PostMapping("/update")
public Result update(@RequestBody Test test) {
try {
testService.update(test.getUsername(),test.getPassword());
return new Result(true,"update成功");
} catch (Exception e) {
return new Result(false,"update失败");
}
}
}