springboot是一个比较流行的框架,个人感觉初学者也并不是非常难搭建不过要深入了解就得自己花功夫去研究了,本人也是学习没多久学生党小白一个,分享一下自己的学习经验,希望能对像我一样初学者一点帮助,话不多说进入正题。
eclipse安装spring插件
使用springboot咱们得先安装一下springboot的插件,这有个链接安装很方便
eclips安装springboot插件
建立springboot项目
安装完插件之后就可以开始我们的项目了
因为是国外的,所以创建这项目的时候可能会不显示出这个界面,多试几次就好了
点击file----new----other----springboot Starter Project
然后next----next----finish 我们的项目就创建好了
pom.xml
这是一个我们需要引入的依赖
<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>1.3.1</version>
</dependency>
<!--mapper-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.2.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<!-- 引入 thymeleaf 模板依赖 -->
<dependency>
<groupId>org.springframework.boot </groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 热部署 -->
<!-- devtools可以实现页面热部署(即页面修改后会立即生效,
这个可以直接在application.properties文件中配置spring.thymeleaf.cache=false来实现) -->
<!-- 实现类文件热部署(类文件修改后不会立即生效),实现对属性文件的热部署。 -->
<!-- 即devtools会监听classpath下的文件变动,并且会立即重启应用(发生在保存时机),
注意:因为其采用的虚拟机机制,该项重启是很快的 -->
<!-- (1)base classloader (Base类加载器):加载不改变的Class,例如:第三方提供的jar包。 -->
<!-- (2)restart classloader(Restart类加载器):加载正在开发的Class。 -->
<!-- 为什么重启很快,因为重启的时候只是加载了在开发的Class,没有重新加载第三方的jar包。 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<!-- optional=true, 依赖不会传递, 该项目依赖devtools;
之后依赖boot项目的项目如果想要使用devtools, 需要重新引入 -->
<optional>true</optional>
</dependency>
在application.properties中添加如下配置
application.properties
#spring.freemarker.cache=false
#spring.thymeleaf.cache=true
spring.devtools.restart.enabled=true
spring.devtools.restart.additional-paths=src/main/java
#\u8D44\u6E90\u6587\u4EF6\u5F00\u653E\u70ED\u90E8\u7F72
#spring.devtools.restart.exclude=static/**,public/**
#spring.devtools.restart.exclude=WEB-INF/**
#######################\u914D\u7F6E\u6587\u4EF6#######################
spring.thymeleaf.prefix = classpath: /templates/
spring.thymeleaf.suffix = .html
spring.thymeleaf.mode = HTML5
spring.thymeleaf.encoding = UTF - 8
spring.thymeleaf.content - type = text / html
spring.thymeleaf.cache = true
restart.include.mapper=/mapper-[\\w-\\.]+jar
restart.include.pagehelper=/pagehelper-[\\w-\\.]+jar
spring.datasource.url=jdbc:mysql://localhost:3306/lisc?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.initial-size=1
spring.datasource.druid.min-idle=1
spring.datasource.druid.max-active=20
spring.datasource.druid.test-on-borrow=true
spring.datasource.druid.stat-view-servlet.allow=true
##\u770B\u597D\u5DE5\u7A0B\u540D
mybatis.type-aliases-package=com.demo.entity
mybatis.mapper-locations=classpath:mapper/*.xml
mapper.mappers=com.demo.utils.MyMapper
mapper.not-empty=false
mapper.identity=MYSQL
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
创建我们需要用的包以及文件其中DempApplication.java是我们的运行文件
他的要求是必须是置顶,也就是说其他包是他的子文件
GeneratorDisplay.java
package com.demo.utils;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
public class GeneratorDisplay {
public void generator() throws Exception{
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//指定 逆向工程配置文件
File configFile = new File("generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
callback, warnings);
myBatisGenerator.generate(null);
}
public static void main(String[] args) throws Exception {
try {
GeneratorDisplay generatorSqlmap = new GeneratorDisplay();
generatorSqlmap.generator();
} catch (Exception e) {
e.printStackTrace();
}
}
}
MyMapper.java
package com.demo.utils;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> {
//TODO
}
generatorConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="MysqlContext" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="com.demo.utils.MyMapper"/>
</plugin>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/lisc"
userId="root"
password="root">
</jdbcConnection>
<!-- 对于生成的pojo所在包 -->
<javaModelGenerator targetPackage="com.demo.entity" targetProject="src/main/java"/>
<!-- 对于生成的mapper所在目录 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/>
<!-- 配置mapper对应的java映射 -->
<javaClientGenerator targetPackage="com.demo.mapper" targetProject="src/main/java"
type="XMLMAPPER"/>
<table tableName="client"></table>
</context>
</generatorConfiguration>
然后还得需要在数据库创建我们的表,我这里已经创建好了
然后就可以直接运行GeneratorDisplay.java文件了,这里直接运行是通过逆向工程直接帮我们创建好所需要的mapper文件以及实体类
然后我们来创建我们需要的server层,以及在DempApplication.java中添加我们的扫描
ClientService.java
package com.demo.service;
import java.util.List;
import com.demo.entity.Client;
public interface ClientService {
public void saveUser(Client u) ;
public void updateUser(Client u);
public void deleteUser(Client u);
public void quer(Client u);
}
ClientServiceImpl.java
package com.demo.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;
import com.demo.entity.Client;
import com.demo.mapper.ClientMapper;
import com.demo.service.ClientService;
import tk.mybatis.mapper.entity.Example;
@Service
public class ClientServiceImpl implements ClientService {
@Autowired
private ClientMapper clientMapper;
@Override
@Transactional(propagation = Propagation.REQUIRED)
public void saveUser(Client u) {
clientMapper.insert(u);
}
@Override
@Transactional(propagation = Propagation.REQUIRED)
public void updateUser(Client u) {
clientMapper.updateByPrimaryKeySelective(u);
}
@Override
@Transactional(propagation = Propagation.REQUIRED)
public void deleteUser(Client u) {
clientMapper.deleteByPrimaryKey(u);
}
@Override
@Transactional(propagation = Propagation.REQUIRED)
public void quer(Client u) {
clientMapper.select(u);
}
}
DemoApplicatioc.java
package com.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
@MapperScan("com.demo.mapper")
@SpringBootApplication
public class DempApplication {
public static void main(String[] args) {
SpringApplication.run(DempApplication.class, args);
}
}
然后我们试着运行一下DemoApplicatioc.java,点击run As springboot可以看到没有啥子问题,然后我们在templates中创建一个index.html,ondex.html,以及在Controller包中创建ClientController.java
index.html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title></title>
</head>
<body>
<br/>
<form th:action="@{/th/index}" th:object="${u}" method="post" th:method="post">
<input type="text" th:field="*{id}" value="用户id:"/>
</br>
<input type="text" th:field="*{name}" value="用户姓名:"/>
</br>
<input type="text" th:field="*{number}" value="用户电话:"/>
</br>
<input type="text" th:field="*{mail}" value="用户e-mail:"/>
</br>
<input type="text" th:field="*{address}" value="用户地址:"/>
</br>
<input type="submit"/>
</form>
<br/>
</body>
</html>
ondex.html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title></title>
</head>
<body>
<h1 th:text=${in}></h1>
</body>
</html>
ClientController.java
package com.demo.controller;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.demo.entity.Client;
import com.demo.service.ClientService;
@Controller
@RequestMapping("th")
public class ClientController {
@Autowired
private ClientService clientsr;
@RequestMapping("/dex")
public String ondex(ModelMap map,Client u) throws Exception {
map.addAttribute("u",u );
return "index";
}
@RequestMapping("/index")
public String index(ModelMap map,Client u) throws Exception {
clientsr.saveUser(u);
map.addAttribute("in","导入成功" );
return "ondex";
}
}
运行输入我们需要插入的信息然后点击提交
再往数据库中看我们看到已经插入了这样一条数据
至此我们的前端到后端已经互通了,至于要深入了解的朋友可以和我一起多多学习,此前端部分呢我采用的是thymeleaf模板,配置文件以及依赖也已经给出了,下面是之前逆向工程中自己导进来的文件
ClientMapper.java
package com.demo.mapper;
import java.util.List;
import com.demo.entity.Client;
import com.demo.utils.MyMapper;
public interface ClientMapper extends MyMapper<Client> {
}
ClientMapper.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="com.demo.mapper.ClientMapper">
<resultMap id="BaseResultMap" type="com.demo.entity.Client">
<!--
WARNING - @mbg.generated
-->
<id column="id" jdbcType="INTEGER" property="id" />
<result column="number" jdbcType="INTEGER" property="number" />
<result column="name" jdbcType="LONGVARCHAR" property="name" />
<result column="mail" jdbcType="LONGVARCHAR" property="mail" />
<result column="address" jdbcType="LONGVARCHAR" property="address" />
</resultMap>
</mapper>
Client.java
package com.demo.entity;
import javax.persistence.*;
public class Client {
@Id
private Integer id;
private Integer number;
private String name;
private String mail;
private String address;
/**
* @return id
*/
public Integer getId() {
return id;
}
/**
* @param id
*/
public void setId(Integer id) {
this.id = id;
}
/**
* @return number
*/
public Integer getNumber() {
return number;
}
/**
* @param number
*/
public void setNumber(Integer number) {
this.number = number;
}
/**
* @return name
*/
public String getName() {
return name;
}
/**
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* @return mail
*/
public String getMail() {
return mail;
}
/**
* @param mail
*/
public void setMail(String mail) {
this.mail = mail;
}
/**
* @return address
*/
public String getAddress() {
return address;
}
/**
* @param address
*/
public void setAddress(String address) {
this.address = address;
}
}
以下为一些更详细的mybatis介绍大家可以看一下非常厉害这位牛人写的,我学习的网站是通过慕课学习的,大家也可以进去学习学习,还有很多的学习网站有兴趣的可以去搜一搜,我就不多说了,一起加油
mybatis详细介绍