Springboot学习(二)
Springboot进行查询
概述
在配置完成后,就要开始进行基本功能的尝试了,首先尝试一下全部查询,逻辑很简单,逐层调用。在简单的全部查询的基础上增加了分页查询,用的是github上的pagehelper插件。
过程
- User类
package com.iron.demo.entity;
public class User {
private Integer id;
private String username;
private String gender;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username == null ? null : username.trim();
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender == null ? null : gender.trim();
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
2.UserMapper类,加上Mapper注解,加上findByPage方法
package com.iron.demo.entityMapper;
import com.github.pagehelper.Page;
import com.iron.demo.entity.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper {
int deleteByPrimaryKey(Integer id);
int insert(User record);
int insertSelective(User record);
User selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
List<User> selectAll();
Page<User> findByPage();
}
3.UserMapper.xml,同上,增加findByPage方法。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.iron.demo.entityMapper.UserMapper" >
<resultMap id="BaseResultMap" type="com.iron.demo.entity.User" >
<id column="Id" property="id" jdbcType="INTEGER" />
<result column="username" property="username" jdbcType="VARCHAR" />
<result column="gender" property="gender" jdbcType="VARCHAR" />
<result column="age" property="age" jdbcType="INTEGER" />
</resultMap>
<sql id="Base_Column_List" >
Id, username, gender, age
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from user
where Id = #{id,jdbcType=INTEGER}
</select>
<select id="findByPage" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from User
</select>
<select id="selectAll" resultMap="BaseResultMap" >
select
<include refid="Base_Column_List" />
from user
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from user
where Id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.iron.demo.entity.User" >
insert into user (Id, username, gender,
age)
values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{gender,jdbcType=VARCHAR},
#{age,jdbcType=INTEGER})
</insert>
<insert id="insertSelective" parameterType="com.iron.demo.entity.User" >
insert into user
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
Id,
</if>
<if test="username != null" >
username,
</if>
<if test="gender != null" >
gender,
</if>
<if test="age != null" >
age,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=INTEGER},
</if>
<if test="username != null" >
#{username,jdbcType=VARCHAR},
</if>
<if test="gender != null" >
#{gender,jdbcType=VARCHAR},
</if>
<if test="age != null" >
#{age,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.iron.demo.entity.User" >
update user
<set >
<if test="username != null" >
username = #{username,jdbcType=VARCHAR},
</if>
<if test="gender != null" >
gender = #{gender,jdbcType=VARCHAR},
</if>
<if test="age != null" >
age = #{age,jdbcType=INTEGER},
</if>
</set>
where Id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.iron.demo.entity.User" >
update user
set username = #{username,jdbcType=VARCHAR},
gender = #{gender,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER}
where Id = #{id,jdbcType=INTEGER}
</update>
</mapper>
4.UserService接口,同上
package com.iron.demo.service;
import com.github.pagehelper.Page;
import com.iron.demo.entity.User;
import java.util.List;
public interface UserService {
int addUser(User user);
User findUserById(int id);
List<User> selectAll();
int updateUser(User user);
int deleteUserById(int id);
Page<User> findByPage(int pageNo, int pageSize);
}
5.UserServiceImpl类,同上,方法里的pageNo是页数,pageSize是一页的数据个数,通过这两个参数进行查询。
package com.iron.demo.serviceimpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.iron.demo.entity.User;
import com.iron.demo.entityMapper.UserMapper;
import com.iron.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service(value="userService")
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
@Override
public int addUser(User user) {
return 0;
}
@Override
public User findUserById(int id) {
return null;
}
@Override
public List<User> selectAll() {
return userMapper.selectAll();
}
@Override
public int updateUser(User user) {
return 0;
}
@Override
public int deleteUserById(int id) {
return 0;
}
@Override
public Page<User> findByPage(int pageNo, int pageSize) {
PageHelper.startPage(pageNo,pageSize);
return userMapper.findByPage();
}
}
6.UserController类
package com.iron.demo.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iron.demo.entity.User;
import com.iron.demo.service.UserService;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
@Controller
public class UserController {
@Resource
private UserService userService;
@RequestMapping(value="/userList",produces = {"application/json;charset=UTF-8"})
public String getAllUsers(@RequestParam(value="pn",defaultValue="1") Integer pn,Model model){
List<User> postIn = userService.findByPage(pn,3);
System.out.println(postIn+"===========");
//用PageInfo对结果进行包装
PageInfo<User> page = new PageInfo<User>(postIn);
model.addAttribute("pageInfo", page);
return "index";
}
}
7.然后启动DemoApplication类,结果如下图