springboot 使用jdbctemplate
package com.jxd.Boot.web;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.PageRequest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jxd.Boot.mybatis.dao.MstudentMapper;
import com.jxd.Boot.mybatis.po.Mstudent;
import com.jxd.Boot.mybatis.po.MstudentExample;
import com.jxd.Boot.po.Student;
import com.jxd.Boot.service.StudentService;
import com.jxd.Boot.util.Pager;
import com.jxd.Boot.util.RenderPager;
@RestController
public class StudentController {
Logger logger = LoggerFactory.getLogger(getClass());
@Resource
private JdbcTemplate jdbcTemplate;
@RequestMapping("/testjdbcadd")
public Object testjdbcadd() {
String sql="INSERT INTO `springboot`.`student` (`teacher_id`, `uid`, `name`, `age`) VALUES ('fdfdff', ?, ?, ?)";
List<Object> args = new ArrayList<Object>();
args.add(UUID.randomUUID().toString());
args.add("金字塔哈哈");
args.add("18");
int rs=jdbcTemplate.update(sql, args.toArray());
return rs;
}
@RequestMapping("/testjdbcdelete")
public Object testjdbcdelete() {
String sql="delete from student where name like ? ";
List<Object> args = new ArrayList<Object>();
args.add("%哈哈%");
int rs=jdbcTemplate.update(sql, args.toArray());
return rs;
}
@RequestMapping("/testjdbcupdate")
public Object testjdbcupdate() {
String sql="update student set age=19 where name like ? ";
List<Object> args = new ArrayList<Object>();
args.add("%金字塔%");
int rs=jdbcTemplate.update(sql, args.toArray());
return rs;
}
/**
* @Description (测试jdbctemplate 分页查询)
* @param no
* @param size
* @return
*/
@RequestMapping("/testjdbcquery")
public Object testjdbc(@RequestParam(defaultValue = "1") Integer no,
@RequestParam(defaultValue = "10") Integer size, String name) {
List<Student> list = new ArrayList<Student>();
String sql = "select * from student where name like ? order by age desc ";
sql = RenderPager.converPageSql(sql, no, size);
List<Object> args = new ArrayList<Object>();
args.add("%" + name + "%");
list = jdbcTemplate.query(sql, new StudentMapper(),args.toArray());
PageRequest page = new PageRequest(no, size);
Pager pager = RenderPager.jdbcconvertPager(list, page, (int) studentService.count());
return pager;
}
class StudentMapper implements RowMapper<Student> {
@Override
public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
// TODO Auto-generated method stub
Student student = new Student();
student.setName(rs.getString("name"));
student.setUid(rs.getString("uid"));
student.setAge(rs.getInt("age"));
return student;
}
}
}