项目用的spring boot + mybatis 搭建的 。IDE 用的 idea
1:新建项目
选几个插件(没选后面也可以在pom.xml文件里面添加)
然后next选择位置,finish 。 然后等项目加载,稍后进入下一步
2:配置
这里配置spring boot,如果没有,点击截图里面的更多按钮。
取个名就好了
进入下一步。文件结构
3:项目结构 ,先搭后台项目结构,前端的后面再介绍
刚建好的项目结构如下:
接下来简历常用的层级结构 controller 、dao、pojo、service
4:项目配置
配置application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/xxw?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
spring.datasource.username=root
spring.datasource.password=xxxx
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis.mapper-locations: classpath:mapper/*.xml
mybatis.type-aliases-package: com.example.mydemo.pojo
server.port=8080
spring.mvc.static-path-pattern=/**
spring.resources.static-locations=classpath:/web/,classpath:/META-INF/resources/,classpath:/resources/,\
classpath:/static/,classpath:/public/,file:${web.upload-path}
第一段配置了数据库相关信息
第二段配置了mapper文件位置,和pojo位置
第三段配置了静态资源位置 主要前端会用到
修改pom.xml,配置需要编译的资源文件
在build标签下面 增加如下配置 :(后面会贴出pom.xml全代码)
这样在编译项目的时候就会将java相关和配置相关的文件编译进去了。
接下来配置mybatis自动生成代码
在pom.xml的plugin 里面增加如下配置
再在resources下建一个配置文件
内容如下:
<?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>
<!--mysql 连接数据库jar 这里选择自己本地位置-->
<classPathEntry location="C:\Users\Administrator\.m2\repository\mysql\mysql-connector-java\8.0.13\mysql-connector-java-8.0.13.jar" />
<context id="testTables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/xxw?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8"
userId="root"
password="xxxxx">
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetProject:生成PO类的位置 -->
<javaModelGenerator targetPackage="com.example.mydemo.pojo"
targetProject="src/main/java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- xml targetProject:mapper映射文件生成的位置
如果maven工程只是单独的一个工程,targetProject="src/main/java"
若果maven工程是分模块的工程,targetProject="所属模块的名称",例如:
targetProject="ecps-manager-mapper",下同-->
<sqlMapGenerator targetPackage="mapper"
targetProject="src/main/resources">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage: java mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.example.mydemo.dao"
targetProject="src/main/java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定数据库表 -->
<table schema="" tableName="comment"></table>
<table schema="" tableName="video"></table>
</context>
</generatorConfiguration>
pom.xml的全代码如下 :
<?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.example</groupId>
<artifactId>mydemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>mydemo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<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.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<!--Mybatis-generator插件,用于自动生成Mapper和POJO-->
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<!--配置文件的位置-->
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
注意到在generatorConfig文件最后面配置的两个数据库表,这两个表需要你的数据库里有。 然后试一下mybatis的自动生成代码
如果没有看到mybatis插件,点一下刷新就好了。运行一下mybatis generator插件,提示成功后,会自动生成pojo , dao , mapper文件。如下:
最后需要我们的项目来扫描支持mybatis,修改MydemoApplication.java
package com.example.mydemo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = {"com.example.mydemo.controller","com.example.mydemo.service"})
@MapperScan(basePackages = {"com.example.mydemo.dao"})
public class MydemoApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(MydemoApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(this.getClass());
}
}
接下来,我们只要关心编写 service和control 。
写一个简单的吧:
VideoService.java
package com.example.mydemo.service;
import com.example.mydemo.dao.VideoMapper;
import com.example.mydemo.pojo.Video;
import com.example.mydemo.pojo.VideoExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class VideoService {
@Autowired
private VideoMapper videoMapper;
public Video getVideoByName(String name){
VideoExample ex = new VideoExample();
VideoExample.Criteria criteria = ex.createCriteria();
criteria.andVideoNameEqualTo(name);
List<Video> videos = videoMapper.selectByExample(ex);
if (null != videos && videos.size()>0){
return videos.get(0);
}else{
return null;
}
}
}
VideoController.java
package com.example.mydemo.controller;
import com.example.mydemo.pojo.Video;
import com.example.mydemo.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/video", method = { RequestMethod.GET, RequestMethod.POST })
public class VideoController {
@Autowired
private VideoService videoService;
@RequestMapping("/getVideo")
@ResponseBody
public Video getComment(String name){
return videoService.getVideoByName(name);
}
}
5:看看成果
在运行项目前,把pom.xml里面mybatis generator插件屏蔽掉,不然mapper文件会重复编译,导致编译不通过 !!!
运行项目,当提示运行成功时 , 浏览器输入地址 :http://localhost:8080/video/getVideo?name=wo 查看查询结果
后台搭建,到此就结束了。