使用springboot进行增删改查
场景启动器:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
配置:
server.port=8080
server.servlet.context-path=/sboot1113_xjx
#mysql
spring.datasource.url=jdbc:mysql://localhost:3306/ms?serverTimezone=UTC&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#mybatis
mybatis.type-aliases-package=com.neusoft.sboot1113_xjx.domain
mybatis.mapperlocations=classpath:mybatis/mapper/*Mapper.xml
pagehelper.helper-dialect=mysql
pagehelper.params=count=countSql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true
实体类
private Integer id;
private String name;
private String sex;
private String idcard;
private String emcard;
private String phone;
private String post;
private Integer sal;
private String bkcard;
mapper.java
public void insertEmployee(Employee employee);
int deleteByPrimaryKey(Integer id);
int updateByPrimaryKey(Employee record);
List<Employee> selectByExample(EmployeeExample example);
Employee selectByPrimaryKey(Integer id);
mapper.java 类 注意在类上加 @Mapper 注解
mapper.xml
<insert id="insertEmployee" parameterType="employee">
INSERT INTO employee (name,sex,idcard,emcard,phone,post,sal,bkcard) VALUES(#{name},#
{sex},#{idcard},#{emcard},#{phone},#{post},#{sal},#{bkcard})
</insert>
<delete id="deleteByPrimaryKey" parameterType="Integer" >
delete from employee
where id = #{id,jdbcType=INTEGER}
</delete>
<update id="updateByPrimaryKeySelective" parameterType="com.neusoft.sboot1113_xjx.domain.Employee" >
update employee
<set >
<if test="name != null" >
name = #{name,jdbcType=VARCHAR},
</if>
<if test="sex != null" >
sex = #{sex,jdbcType=CHAR},
</if>
<if test="idcard != null" >
idcard = #{idcard,jdbcType=VARCHAR},
</if>
<if test="emcard != null" >
emcard = #{emcard,jdbcType=VARCHAR},
</if>
<if test="phone != null" >
phone = #{phone,jdbcType=VARCHAR},
</if>
<if test="post != null" >
post = #{post,jdbcType=VARCHAR},
</if>
<if test="sal != null" >
sal = #{sal,jdbcType=INTEGER},
</if>
<if test="bkcard != null" >
bkcard = #{bkcard,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}</update>
<select id="selectByExample" resultMap="BaseResultMap" parameterType="com.neusoft.sboot1113_xjx.domain.EmployeeExample" >
select
<if test="distinct" >
distinct
</if>
<include refid="Base_Column_List" />
from employee
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null" >
order by ${orderByClause}
</if>
</select>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="Integer" >
select
<include refid="Base_Column_List" />
from employee
where id = #{id,jdbcType=INTEGER}
</select>
service.java
public interface EmployeeService {
List<Employee> selectByExample(EmployeeExample example);
public void insertEmployee(Employee employee);
Employee selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(Employee record);
int deleteByPrimaryKey(Integer id);
}
service实现类
@Service
public class EmployeeServiceImpl implements EmployeeService{
@Autowired
public EmployeeMapper employeeMapper;
@Override
public List<Employee> selectByExample(EmployeeExample example) {
// TODO Auto-generated method stub
return employeeMapper.selectByExample(example);
}
@Override
public void insertEmployee(Employee employee) {
// TODO Auto-generated method stub
employeeMapper.insertEmployee(employee);
}
@Override
public Employee selectByPrimaryKey(Integer id) {
// TODO Auto-generated method stub
return employeeMapper.selectByPrimaryKey(id);
}
@Override
public int updateByPrimaryKeySelective(Employee record) {
// TODO Auto-generated method stub
employeeMapper.updateByPrimaryKeySelective(record);
return 1;
}
@Override
public int deleteByPrimaryKey(Integer id) {
// TODO Auto-generated method stub
employeeMapper.deleteByPrimaryKey(id);
return 1;
}
}
controller
@Controller
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@RequestMapping("/list")
public String list(Model model,@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum) {
System.out.println("list():");
Page page = PageHelper.startPage(pageNum, 5);
List<Employee> employeeList = employeeService.selectByExample(null);
PageInfo<Employee> pageInfo = new PageInfo<>(employeeList,5);
model.addAttribute("pageCount", page.getPages());
model.addAttribute("pageNum", page.getPageNum());
model.addAttribute("pageSize", page.getPageSize());
model.addAttribute("pageInfo",pageInfo);
return "list";
}
@RequestMapping("/add")
public String add() {
return "add";
}
@RequestMapping("/addSeckill")
public String addProduct(Employee employee) {
System.out.println("seckill"+employee);
employeeService.insertEmployee(employee);
return "redirect:list";
}
@RequestMapping("/toUpdate")
public ModelAndView toUpdate(Integer id) {
System.out.println("update id:"+id);
ModelAndView mav=new ModelAndView();
mav.addObject("employee",employeeService.selectByPrimaryKey(id));
mav.setViewName("update");
return mav;
}
@RequestMapping("/update")
public String update(Employee employee) {
System.out.println("update id:"+employee);
employeeService.updateByPrimaryKeySelective(employee);
return "redirect:list";
}
@RequestMapping("/delete")
public String delete(Integer id) {
System.out.println("delete id:"+id);
employeeService.deleteByPrimaryKey(id);
return "redirect:list";
}
}
list.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table>
<a th:href="@{/add}">添加数据</a>
<tr>
<th>id</th>
<th>name</th>
<th>sex</th>
<th>idcard</th>
<th>emcard</th>
<th>phone</th>
<th>post</th>
<th>sal</th>
<th>bkcard</th>
</tr>
<tr th:each="e:${pageInfo.list}">
<td th:text="${e.id}"></td>
<td th:text="${e.name}"></td>
<td th:text="${e.sex}"></td>
<td th:text="${e.idcard}"></td>
<td th:text="${e.emcard}"></td>
<td th:text="${e.phone}"></td>
<td th:text="${e.post}"></td>
<td th:text="${e.sal}"></td>
<td th:text="${e.bkcard}"></td>
<td><a th:href="@{/delete(id=${e.id})}">delete</a>||<a th:href="@{/toUpdate(id=${e.id})}">toUpdate</a></td><br/>
</tr>
<tr>
<td colspan="5">总页数<span th:text="${pageCount}"></span>,当前第<span th:text="${pageNum}"></span>页,每页显示<span th:text="${pageSize}"></span>条数据</td>
<!-- <a th:href="@{/list(pageNum=${pageInfo.hasPreviousPage}?${pageInfo.pages}:1)}">上一页</a> -->
<th:block th:each="i :${#numbers.sequence(1, pageInfo.pages)}">
<a th:href="@{/list(pageNum=${i})}">
<span th:class="${pageInfo.pageNum == i}">
<th:block th:text="${i}"></th:block>
</span>
</a>
</th:block>
<!-- <a th:href="@{/list(pageNum=${pageInfo.hasNextPage}?${pageInfo.nextPage}:${pageInfo.pages})}">下一页</a> -->
</tr>
</table>
</body>
</html>
add.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form th:action="@{/addSeckill}" method="post">
name:<input type="text" name="name"/><br>
sex:<input type="text" name="sex"/><br>
idcard:<input type="text" name="idcard"/><br>
emcard:<input type="text" name="emcard"/><br>
phone:<input type="text" name="phone"/><br>
post:<input type="text" name="post"/><br>
sal:<input type="text" name="sal"/><br>
bkcard:<input type="text" name="bkcard"/><br>
<input type="submit" value="add">
</form>
</body>
</html>
update.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="" th:action="@{/update}" method="post" th:object="${employee}">
id:<input type="text" name="" value="" th:field="*{id}"/><br>
name:<input type="text" name="" value="" th:field="*{name}"/><br>
idcard:<input type="text" name="" value="" th:field="*{idcard}"/><br>
emcard:<input type="text" name="" value="" th:field="*{emcard}"/><br>
phone:<input type="text" name="" value="" th:field="*{phone}"/><br>
post:<input type="text" name="" value="" th:field="*{post}"/><br>
sal:<input type="text" name="" value="" th:field="*{sal}"/><br>
bkcard:<input type="text" name="" value="" th:field="*{bkcard}"/><br>
<input type="submit" value="update"/>
</form>
</body>
</html>
还没有评论,来说两句吧...