Springboot + MyBatis to realize the additions and deletions of employees. New Springboot project using idea
File-->new-->project-->spring assistant-->next--> Modify registration, project name and other information-->next-->
Select the web in the Web, select MySQL, Jdbs, mybatis-->next-->finish in SQL
Second, modify the Pom.xml file "before the operation has been imported springboot, MySQL, JDBC, mybatis related package information, there is no special need to modify the Pom.xml file" Three, the new package and the corresponding interface and class
1. New Package
2. Write Controller
package cn.qiu.controller;import cn.qiu.entity.Emp;import cn.qiu.service.EmpService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import javax.annotation.Resource;import java.util.List;@Controllerpublic class EmpController { @Resource EmpService empService; @RequestMapping("/a") public String a(){ return "emp"; } @ResponseBody @RequestMapping("/aa") public List<Emp> aa(){ return empService.findAll(); }}
3. Write Service and Serviceimpl
package cn.qiu.service;import cn.qiu.entity.Emp;import java.util.List;public interface EmpService { public List<Emp> findAll();}
package cn.qiu.service.impl;import cn.qiu.dao.EmpDao;import cn.qiu.entity.Emp;import cn.qiu.service.EmpService;import org.springframework.stereotype.Service;import javax.annotation.Resource;import java.util.List;@Service(value = "userService")public class EmpServiceImpl implements EmpService { @Resource EmpDao empDao; @Override public List<Emp> findAll() { return empDao.findAll(); }}
4. Writing DAO
package cn.qiu.dao;import cn.qiu.entity.Emp;import org.apache.ibatis.annotations.Mapper;import java.util.List;@Mapperpublic interface EmpDao { public List<Emp> findAll();}
5, Write Mapper
<?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="cn.qiu.dao.EmpDao" > <select id="findAll" resultType="cn.qiu.entity.Emp"> select name,sec sex,agg age,addre address from Person; </select></mapper>
Iv. New Database and Table v. New SPRINGBOOT configuration file application.yml
#设置Tomcat端口,默认8080server.port=8081#设置项目ContextPath#server.context-path=/#设置Tomcat编码server.tomcat.uri-encoding=UTF-8#设置视图解析器路径spring.mvc.view.prefix=/WEB-INF/JSP/#设置视图解析器后缀spring.mvc.view.suffix=.jsp#数据库配置spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=truespring.datasource.username=rootspring.datasource.password=qiuhongchijuan12spring.datasource.driver-class-name=com.mysql.jdbc.Driver#配置.xml文件路径(mapper文件位置)mybatis.mapper-locations=classpath:mapper/*.xml#配置模型路径(实体类的位置)mybatis.type-aliases-package=cn.qiu.entity
Six, write static page seven, test
Springboot + MyBatis to make additions and deletions to employees