Today we build a simple MAVEN project based on the Springboot SSM (Spring + SPRINGMVC + mybatis) that uses a database of MySQL. Well, the nonsense is not much to say. Start to get up. Final project directory structure
Create Process 1. Create Switch springbootapplication
To create a quick. We created the structure using idea's own creation springboot, and of course it was possible to create ordinary Web projects. (Students who use eclipse can create their own directory structure according to the diagram of a meeting)
1.1 Create the project and follow the diagram to select it
1.2
1.3
1.4
Okay, our initial project is done. The project structure is shown in the figure below, where ssmspringboot2application is our switch file.
In fact, now a simple springboot project we have built. What the. Don't believe it. , you can run ssmspringboot2application this file. You will report that you did not configure the DataSource, we are shown below to perform a configuration to complete a simple Springboot project to run through
Spring.datasource.url=jdbc:mysql://localhost:3306/maxrocky
Spring.datasource.username=root
spring.datasource.password=123456
Spring.datasource.driverclassname=com.mysql.jdbc.driver
Spring.jpa.database = MySQL
Account password of course you want to configure your own. OK Project running up. The simplest Springboot project is completed. It's over. No, no, no, we have to build the SSM project.
Now we begin to join our SSM. 2. Simple description of directory structure
Before showing the students how to integrate the SSM into the project. First with the students to talk about the directory structure to facilitate the understanding of all students.
2.1 java
That's not much to say. The Java file we wrote.
2.2 Resources
Springboot Advocate no XML configuration, but still need some of the most basic information configuration, such as the SQL account password settings, in the concise your account password or need to configure your own drip, it is no way to help you automatically generated. Therefore, general configuration files are placed under resources. The specific default generated files are what to do and what resources to put under what files can see I have written a piece on the role of the folder Springboot directory structure detailed
2.3 Switch File
The Ssmspringboot2application file is the core switch of the springboot. 3. Integration
Requirements: Query from the database for a user of all the information returned to the front page
Okay, here's a simple statement. Start integration. is also based on the development of the most basic three-tier framework for development. But for the convenience of omitting the service layer.
After adding the structure as follows, I put it in the comments of the Code
Database is as follows
3.1 User (creates an object to receive data from the query)
Package com.example.demo.domain;
/** * Created by Beyondli on 2017/6/19.
* * Public class User {private Integer ID;
Private String username;
Private Integer age;
Private Integer CustomerID;
Public Integer GetId () {return id;
The public void SetId (Integer id) {this.id = ID;
Public String GetUserName () {return username;
} public void Setusername (String username) {this.username = username;
Public Integer Getage () {return age;
public void Setage (Integer age) {this.age = age;
Public Integer Getcustomerid () {return CustomerID;
The public void Setcustomerid (Integer customerid) {This.customerid = CustomerID; @Override public String toString () {return "user{" + "id=" + ID + ", u Sername= ' "+ username + ' \" + ", age=" + Age + ", customerid=" + CustOmerid + '} ';
}
}
3.2 DAO Layer Creation interface
Package Com.example.demo.dao;
Import Com.example.demo.domain.User;
Import Org.apache.ibatis.annotations.Mapper;
Import org.springframework.stereotype.Repository;
/**
* Created by Beyondli on 2017/6/19.
* *
@Mapper //declaration is a Mapper, with springbootapplication in @mapperscan two select a write can
@Repository
public Interface Usermapper {
User selectuserbyname (String name);
}
3.3 Controller Layer
Package Com.example.demo.controller;
Import Com.example.demo.dao.UserMapper;
Import Com.example.demo.domain.User;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RestController;
/**
* Created by Beyondli on 2017/6/19.
*//
proof is controller layer and returns JSON
@RestController public
class Usercontroller {
//Dependency Injection
@ autowired
usermapper usermapper;
@RequestMapping (value = "cs") public
User cs () {
//Call DAO layer
User user = Usermapper.selectuserbyname (" Beyondli ");
return user;
}
3.4 Main Switch Ssmspringboot2application
Package Com.example.demo;
Import Org.mybatis.spring.annotation.MapperScan;
Import org.springframework.boot.SpringApplication;
Import org.springframework.boot.autoconfigure.SpringBootApplication;
Import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@EnableTransactionManagement//Open Transaction Management
@MapperScan ("Com.example.demo.dao")// @mapper two with the DAO layer can be selected (the main function is to sweep the package) public
class Ssmspringboot2application {public
static void Main (string[] args) {
Springapplication.run (ssmspringboot2application.class, args);
}
}
We typically write SQL to an XML configuration file based on MyBatis. Now we're going to add a map
3.5 creating the corresponding mapper map
<?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.example.demo.dao.UserMapper" >
<select id= "Selectuserbyname" User ">
SELECT * from user WHERE username = #{name}
</select>
</mapper>
3.6 key points, do not run the project. Need to configure Application.properties
Spring.datasource.url=jdbc:mysql://localhost:3306/maxrocky
Spring.datasource.username=root
spring.datasource.password=123456
Spring.datasource.driverclassname=com.mysql.jdbc.driver
spring.jpa.database = mysql
#Mybatis扫描
mybatis.mapper-locations=classpath*:mapper/*.xml
#起别名. You can omit the full path of resulttype in XML that writes MyBatis
Mybatis.type-aliases-package=com.example.demo.domain
3.7 Pom file. We also need to manually add a dependency to use the mapper annotation
<?xml version= "1.0" encoding= "UTF-8"?> <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/xsd/maven-4.0.0.xsd "> <modelVersion>4.0.0</modelVersion> <groupid>com.exam Ple</groupid> <artifactId>ssmspringboot2</artifactId> <version>0.0.1-snapshot</ version> <packaging>jar</packaging> <name>ssmspringboot2</name> <description> ;D Emo project for Spring boot</description> <parent> <groupid>org.springframework.boot</ Groupid> <artifactId>spring-boot-starter-parent</artifactId> <VERSION>1.5.4.RELEASE&L T;/version> <relativePath/> <!--lookup parent from repository--> </parent> <pro Perties> <project.build.sourceencoding>utf-8</project.build.sourceencoding> <project.reporting.outputencoding>utf-8</ Project.reporting.outputencoding> <java.version>1.8</java.version> </properties>
;d ependencies> <dependency> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starter-jdbc< ;/artifactid> </dependency> <dependency> <groupid>org.springframework.boo
T</groupid> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactid>mysql-connector-ja Va</artifactid> <scope>runtime</scope> </dependency> <dependency> <groupid>org.springframework .boot</groupid> <artifactId>spring-boot-starter-test</artifactId> <scope>t Est</scope> </dependency> <!--new required dependencies--> <dependency> <grou Pid>org.mybatis.spring.boot</groupid> <artifactid>mybatis-spring-boot-starter</artifactid>
; <version>1.1.1</version> </dependency> </dependencies> <build>
;p lugins> <plugin> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins>
</build> </project>
Sprinkle flowers ... End... Done.
Explanations of some of the annotations and the configuration of some paths are well explained in the code. I believe that if you read carefully reading will be able to understand and successfully run the project.
The above views are personal understanding, if there are deficiencies or errors, I hope that the common growth