Springboot + MyBatis to make additions and deletions to employees

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.