系列教程都是從網絡上收集和本人的理解所編輯而成,僅供廣大愛好者學習所用,請尊重本人的勞動成果。歡迎評論指正和轉帖!(請保留連接謝謝!)
一、CatController.java
package com.fs;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CatController {
@Autowired
private CatService catService;
@RequestMapping("/save")
public Cat save() {
Cat cat = new Cat();
cat.setCat_name("王武");
cat.setCat_age(11);
catService.save(cat);
return cat;
}
@RequestMapping("/update")
public Cat update() {
Cat cat = new Cat();
cat.setId(2);
cat.setCat_name("wangwu");
cat.setCat_age(11);
catService.update(cat);
return cat;
}
@RequestMapping("/delete")
public Cat delete() {
Cat cat = new Cat();
cat.setId(2);
catService.delete(cat);
return cat;
}
@RequestMapping("/select")
public List<Cat> select(Cat cat) {
return catService.select(cat);
}
@RequestMapping("/select2")
public List<Cat> select2(Cat cat) {
return catService.select2(cat);
}
}
二、CatService.java
package com.fs;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class CatService {
@Autowired
private CatMappper catMappper;
@Transactional // 添加事務.
public void save(Cat cat) {
catMappper.save(cat);
}
@Transactional // 添加事務.
public void update(Cat cat) {
catMappper.update(cat);
}
@Transactional // 添加事務.
public void delete(Cat cat) {
catMappper.delete(cat);
}
public List<Cat> select(Cat cat) {
return catMappper.select(cat);
}
public List<Cat> select2(Cat cat) {
return catMappper.select2(cat);
}
}
三、CatMappper.java
package com.fs;
import java.util.List;
import org.apache.ibatis.annotations.DeleteProvider;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.UpdateProvider;
public interface CatMappper {
@InsertProvider(type = CatSqlProvider.class, method = "save")
@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id") // id自動增長
@Results({ @Result(property = "updateTime", column = "update_time") })
public void save(Cat cat);
@UpdateProvider(type = CatSqlProvider.class, method = "update")
@Results({ @Result(property = "updateTime", column = "update_time") })
public void update(Cat cat);
@DeleteProvider(type = CatSqlProvider.class, method = "delete")
@Results({ @Result(property = "updateTime", column = "update_time") })
public void delete(Cat cat);
@SelectProvider(type = CatSqlProvider.class, method = "select")
@Results({ @Result(property = "updateTime", column = "update_time") })
public List<Cat> select(Cat cat);
@SelectProvider(type = CatSqlProvider.class, method = "select2")
@Results({ @Result(property = "updateTime", column = "update_time") })
public List<Cat> select2(Cat cat);
}
四、CatSqlProvider.java
package com.fs;
import org.apache.ibatis.jdbc.SQL;
public class CatSqlProvider {
public String select(Cat cat) {
StringBuffer sql = new StringBuffer("select * from cat where 1=1 ");
if (cat.getCat_name() != null) {
sql.append("and cat_name = #{cat_name}");
} else if (cat.getCat_age() != 0) {
sql.append("and cat_age = #{cat_age}");
}
return sql.toString();
}
public String select2(Cat cat) {
return new SQL() {
{
SELECT("id,cat_name,cat_age");
FROM("cat");
if (cat.getCat_name() != null) {
WHERE("cat_name = #{cat_name}");
} else if (cat.getCat_age() != 0) {
WHERE("cat_age = #{cat_age}");
}
}
}.toString();
}
public String save(Cat cat) {
return new SQL() {
{
INSERT_INTO("cat");
VALUES("cat_name", "#{cat_name}");
VALUES("cat_age", "#{cat_age}");
}
}.toString();
}
public String update(Cat cat) {
return new SQL() {
{
UPDATE("cat");
if (cat.getCat_name() != null) {
SET("cat_name = #{cat_name}");
} else if (cat.getCat_age() != 0) {
SET("cat_age = #{cat_age}");
}
WHERE("id = #{id}");
}
}.toString();
}
public String delete(Cat cat) {
return new SQL() {
{
DELETE_FROM("cat");
WHERE("id = #{id}");
}
}.toString();
}
}
五、其它和前面項目一樣