1. Create a new project
new -> maven project -> next -> next -> search webapp in the filter -> group id, Artifact id -> complete.
2, the configuration file pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.mg39.ssm</groupId> <artifactId>test03</artifactId> <packaging>war</packaging> <version>SNAPSHOT-0.0.1 </ Version > < name > TEST03 Maven Webapp </ name > < url > http://maven.apache.org </ url > <-! The first step in spring officially called stater poms, it We can provide the dependency management, that is, dependency management, after the introduction of the statement when you do not need other version of the dependency but mybatis need? ? ? ? ? -> < parent > < the groupId > org.springframework.boot </ the groupId > < the artifactId > Spring-Boot-Starter-parent < <version>1.5.3.RELEASE</version> </parent> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <!-- <version>4.0</version> --> <scope>test</scope> </dependency> <!- ->Step two official explanation spring-boot-start-web contains a tomcat and other characteristics of the spring webmvc and web development <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--添加MySQL的jar --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!--添加mybatis的jar --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!--添加模板框架thymeleaf --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!--模板框架依赖的jar --> <dependency> <groupId>net.sourceforge.nekohtml</groupId> <artifactId>nekohtml</artifactId> <!-- <version>1.9.22</version> --> </dependency> </dependencies> <build> <test03>finalName</ FinalName > <-! The third step if we are to start direct Main spring, then the following plugin must be added, otherwise it can not be started. If you use the maven spring-boot: run, then you do not need this configuration. -> < plugins > < plugin > < the groupId > org.springframework.boot </ the groupId > < the artifactId > Spring-Boot-Maven-plugin </ the artifactId > </ plugin > </ plugins > </ Build > </ Project >
3, write test classes (package name must be a parent package of other packages, or can not enter the controller class)
Test class is the entry procedure. After writing projects such right -> Java Application can run.
Test03.java
Package Penalty for cn.mg39.ssm; // this package is the parent package for all other packages, that is, all the other packages should begin with cn.mg39.ssm, such as cn.mg39.ssm.dao Import org.springframework.boot.SpringApplication ; Import org.springframework.boot.autoconfigure.SpringBootApplication; Import org.springframework.web.bind.annotation.RequestMapping; Import org.springframework.web.bind.annotation.RestController; / ** * Add comment * / @RestController @SpringBootApplication / ** * test class name casually written, Test03 because this is my third the Boot * @author zhangzimu * * / public class TEST03 { / ** * When the project up and running in the browser input http: // localhost: 8080 / login . Helloworld page will have the words. * @Return * / @RequestMapping ( "/ Login" ) public String Login () { return "the HelloWorld" ; } public static void main (String [] args) { SpringApplication.run (. TEST03 class , args); } }
4, building entity classes (SysGroup)
SysGroup.java
package cn.mg39.ssm.entity; /** * 分组实体 * */ public class SysGroup { private Integer id; //分组id private String name; //组名 private String remark; //备注 public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } public SysGroup() { super(); // TODO Auto-generated constructor stub } public SysGroup(Integer id, String name, String remark) { super(); this.id = id; this.name = name; this.remark = remark; } public SysGroup(Integer id) { super(); this.id = id; } @Override public String toString() { return "SysGroup [id=" + id + ", name=" + name + ", remark=" + remark + "]"; } }
5, Dao Interface (SysGroupDao)
SysGroupDao.java
package cn.mg39.ssm.dao; import org.apache.ibatis.annotations.Mapper; import cn.mg39.ssm.entity.SysGroup; @Mapper public interface SysGroupDao { public Integer insert(SysGroup sysGroup); }
6, placed mapper.xml (SysGroupDaoMapper.xml)
01, in the main / resources folder create a new folder for managing mapper mapper file
02, written in the mapper SysGroupDaoMapper.xml
<?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.mg39.ssm.dao.SysGroupDao"> <insert id="insert" keyColumn="id" keyProperty="id" parameterType="sysGroup"> insert into sys_group values(null,#{name},#{remark}) </insert> </mapper>
7, service interfaces (SysGroupService)
SysGroupService.java
package cn.mg39.ssm.service; import cn.mg39.ssm.entity.SysGroup; /** *service接口 */ public interface SysGroupService { public Boolean add(SysGroup sysGroup); }
8, service implementation class (SysGroupServiceImpl)
SysGroupServiceImpl.java
Package cn.mg39.ssm.service.impl; Import org.springframework.beans.factory.annotation.Autowired; Import org.springframework.stereotype.Service; Import cn.mg39.ssm.dao.SysGroupDao; Import cn.mg39.ssm .entity.SysGroup; Import cn.mg39.ssm.service.SysGroupService; // <the bean ID = "sysGroupService" class = "cn.mg39.ssm.service.impl.SysGroupService"> @Service ( "sysGroupService") // to achieve the same functionality sentence above public class SysGroupServiceImpl the implements SysGroupService { @Autowired // automatic injector Private sysGroupDao sysGroupDao; @Override public Boolean add(SysGroup sysGroup) { return sysGroupDao.insert(sysGroup) > 0; } }
9, write controller class
SysGroupController.java
Package cn.mg39.ssm.controller; Import org.springframework.beans.factory.annotation.Autowired; Import org.springframework.stereotype.Controller; Import org.springframework.web.bind.annotation.RequestMapping; Import cn.mg39.ssm .entity.SysGroup; Import cn.mg39.ssm.service.SysGroupService; @Controller public class SysGroupController { @Autowired // automatically injected SysGroupService Private SysGroupService sysGroupService; // display index.html after the browser address bar port not write path ( prefixes and suffixes are arranged in application.yml) @RequestMapping ( "/" ) publicIndex String () { return "index"; // CLASSPATH: /templates/index.html } @RequestMapping ( "/ addGroup") // Data submitted index.html addGroup pass, then judgment jump, jump pages The above principles and the same public String addGroup (the sysGroup the sysGroup) { IF (sysGroupService.add (the sysGroup)) { return "Success" ; } the else { return "Fail" ; } } }
10, written application.yml files in resources
application.yml (mybatis configuration parameters and for database connections required)
#spring configuration application.yml # / Templates / success.html the Spring: Profiles: the Active: dev # xml format and is almost equivalent to single-label Thymeleaf: Cache: Cache is closed when false # development, or can not see the page in real-time mode : LEGACYHTML5 # non-strict the HTML encoding: UTF-. 8 the servlet: Content-type: text / HTML prefix / templates / #controller value in return: prefix: CLASSPATH suffix: .html # suffix datasource: # the following are connected to the database the relevant parameters url: jdbc: MySQL: // localhost: 3306 / hr_oa characterEncoding = utf8? & useSSL = false username: root password: root Driver-class-name: com.mysql.jdbc.Driver the mybatis: the configuration parameters #myBatis type-aliases-package: cn.mg39.ssm.entity # scanning package (forget almost the same) Mapper-locations: the CLASSPATH:. Mapper / * #CRUD xml configuration file (under mapper)
11, written in HTML files
New templates files in the main / resources folder (as prefix templates), written in HTML inside
index.html
INPUT name and attribute values corresponding to the same entities
the success.html
fail.html
12, running in the test class
13, the browser http: // localhost: 8080 /