springboot + mybatis 前后端分离项目的搭建 适合在学习中的大学生
人生如戏,戏子多半掉泪!
我是一名大四学生,刚进入一家软件件公司实习,虽说在大学中做过好多个实训项目,都是自己完成,没有组员的配合。但是在这一个月的实习中,我从以前别人教走到了现在的自学,成长很多。
以前做过的项目都是springmvc+mybatis,到了公司项目里用的是springboot+mybatis。这两个框架很相似,但是如果你不注重细节,你会输的很惨。细节决定成败,你们相信的!
1、创建maven项目(以eclipse为例,以后再为大家介绍idea)
(1)File—New—other—Mavenproject
(2)选择第二个选项 Use default workspace location 然后 next
(3)直接下一步到这个页面 然后Finish(由于是前后端分离,我们先不需要创建web项目)
(4)此时项目结构目录如图,我们还需要创建src/main/resource文件夹
(5)选择项目 点击 new——sourceFolder ,输入路径文件名
(5)点击App.java 运行 出现HelloWord说明项目创建成功
2、框架的搭建(springboot+mybatis)
(1) springboot和springMVC的区别:springboot简化了mvc 繁琐的配置文件 注解基本相同
springboot只用一个配置文件 如图 application-dev.yml
(2)application-dev.yml的配置
1 #日志输出级别:DEBUG。为了可以打印sql语句 2 logging: 3 level: 4 root: DEBUG 5 org.springframework.web: DEBUG 6 #数据库的连接 7 druid: 8 url: jdbc:mysql://localhost:3306/mall?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true 9 username: root 10 password: root 11 initial-size: 1 12 min-idle: 1 13 max-active: 20 14 test-on-borrow: true 15 16 #mybatis mapper的扫描 17 mybatis: 18 mapper-locations: classpath:mapper/*.xml 19 20 #对mybatis内置的简答sql语句的继承 21 mapper: 22 mappers: 23 - com.tenio.base.BaseMapper 24 not-empty: false 25 identity: MYSQL 26 27 #分页 28 pagehelper: 29 helperDialect: mysql 30 reasonable: true 31 supportMethodsArguments: true 32 params: count=countSql
(3)pom.xml配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>com.isoft</groupId> 7 <artifactId>mall</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <name>mall</name> 12 <description>Demo project for Spring Boot</description> 13 14 <parent> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-parent</artifactId> 17 <version>1.5.4.RELEASE</version> 18 <relativePath/> <!-- lookup parent from repository --> 19 </parent> 20 21 <properties> 22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 24 <java.version>1.8</java.version> 25 </properties> 26 27 <dependencies> 28 <dependency> 29 <groupId>org.springframework.boot</groupId> 30 <artifactId>spring-boot-starter-web</artifactId> 31 </dependency> 32 33 <dependency> 34 <groupId>org.springframework.boot</groupId> 35 <artifactId>spring-boot-starter-test</artifactId> 36 <scope>test</scope> 37 </dependency> 38 39 <!--mysql的连接--> 40 <dependency> 41 <groupId>mysql</groupId> 42 <artifactId>mysql-connector-java</artifactId> 43 <version>5.1.39</version> 44 </dependency> 45 46 <!--mybatis--> 47 <dependency> 48 <groupId>org.mybatis.spring.boot</groupId> 49 <artifactId>mybatis-spring-boot-starter</artifactId> 50 <version>1.1.1</version> 51 </dependency> 52 53 54 <dependency> 55 <groupId>org.slf4j</groupId> 56 <artifactId>slf4j-api</artifactId> 57 <version>1.7.21</version> 58 </dependency> 59 <!--mapper--> 60 <dependency> 61 <groupId>tk.mybatis</groupId> 62 <artifactId>mapper-spring-boot-starter</artifactId> 63 <version>1.1.1</version> 64 </dependency> 65 <!--pagehelper--> 66 <dependency> 67 <groupId>com.github.pagehelper</groupId> 68 <artifactId>pagehelper-spring-boot-starter</artifactId> 69 <version>1.1.1</version> 70 </dependency> 71 72 <dependency> 73 <groupId>com.alibaba</groupId> 74 <artifactId>druid</artifactId> 75 <version>1.0.11</version> 76 </dependency> 77 <!-- JJWT --> 78 <dependency> 79 <groupId>io.jsonwebtoken</groupId> 80 <artifactId>jjwt</artifactId> 81 <version>0.6.0</version> 82 </dependency> 83 84 <dependency> 85 <groupId>org.springframework.boot</groupId> 86 <artifactId>spring-boot-devtools</artifactId> 87 <optional>true</optional> 88 </dependency> 89 90 <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> 91 <dependency> 92 <groupId>com.alibaba</groupId> 93 <artifactId>fastjson</artifactId> 94 <version>1.2.31</version> 95 </dependency> 96 97 98 <!-- commons --> 99 <dependency> 100 <groupId>commons-fileupload</groupId> 101 <artifactId>commons-fileupload</artifactId> 102 <version>1.3.1</version> 103 </dependency> 104 105 <dependency> 106 <groupId>commons-io</groupId> 107 <artifactId>commons-io</artifactId> 108 <version>2.4</version> 109 </dependency> 110 111 <dependency> 112 <groupId>commons-beanutils</groupId> 113 <artifactId>commons-beanutils</artifactId> 114 <version>1.9.2</version> 115 </dependency> 116 117 <dependency> 118 <groupId>commons-logging</groupId> 119 <artifactId>commons-logging</artifactId> 120 <version>1.2</version> 121 </dependency> 122 123 <dependency> 124 <groupId>commons-lang</groupId> 125 <artifactId>commons-lang</artifactId> 126 <version>2.6</version> 127 </dependency> 128 129 <dependency> 130 <groupId>commons-collections</groupId> 131 <artifactId>commons-collections</artifactId> 132 <version>3.2.2</version> 133 </dependency> 134 135 136 <dependency> 137 <groupId>org.apache.commons</groupId> 138 <artifactId>commons-lang3</artifactId> 139 <version>3.4</version> 140 </dependency> 141 142 <dependency> 143 <groupId>commons-logging</groupId> 144 <artifactId>commons-logging-api</artifactId> 145 <version>1.1</version> 146 </dependency> 147 <!-- commons end--> 148 149 <!--siteMesh--> 150 <dependency> 151 <groupId>org.sitemesh</groupId> 152 <artifactId>sitemesh</artifactId> 153 <version>3.0.0</version> 154 </dependency> 155 156 <dependency> 157 <groupId>org.apache.directory.studio</groupId> 158 <artifactId>org.apache.commons.codec</artifactId> 159 <version>1.8</version> 160 </dependency> 161 162 <!--pinyin4j--> 163 <dependency> 164 <groupId>com.belerweb</groupId> 165 <artifactId>pinyin4j</artifactId> 166 <version>2.5.0</version> 167 </dependency> 168 169 170 171 <dependency> 172 <groupId>org.apache.poi</groupId> 173 <artifactId>poi</artifactId> 174 <version>3.15</version> 175 </dependency> 176 177 <dependency> 178 <groupId>org.apache.poi</groupId> 179 <artifactId>poi-ooxml</artifactId> 180 <version>3.15</version> 181 </dependency> 182 183 <dependency> 184 <groupId>org.apache.poi</groupId> 185 <artifactId>poi-ooxml-schemas</artifactId> 186 <version>3.15</version> 187 </dependency> 188 189 190 </dependencies> 191 192 <build> 193 <plugins> 194 <plugin> 195 <groupId>org.springframework.boot</groupId> 196 <artifactId>spring-boot-maven-plugin</artifactId> 197 </plugin> 198 199 200 <plugin> 201 <groupId>org.mybatis.generator</groupId> 202 <artifactId>mybatis-generator-maven-plugin</artifactId> 203 <version>1.3.2</version> 204 <configuration> 205 <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile> 206 <overwrite>true</overwrite> 207 <verbose>true</verbose> 208 </configuration> 209 <dependencies> 210 <dependency> 211 <groupId>mysql</groupId> 212 <artifactId>mysql-connector-java</artifactId> 213 <version>5.1.39</version> 214 </dependency> 215 </dependencies> 216 </plugin> 217 </plugins> 218 </build> 219 220 221 </project>
(4)然后把pom.xml 里的jar包从中央仓库下载到本地 点击Maven install
当看到BUILD SUCCESS 时,说明jar包已经下载成功
(5)点击项目—Maven Dependencies 可以看到刚才下载的jar包和路径
(5)修改App.java代码 点击运行
1 @SpringBootApplication 2 @EnableTransactionManagement 3 public class App 4 { 5 public static void main( String[] args ) 6 { 7 SpringApplication.run(App.class, args); 8 } 9 }
(7)run 看到下图,说明springboot+mybatis 框架搭建成功
- git的使用(相对SVN操作相对较难,也是公司项目使用的主流趋势,不会它你将无法工作,先不说github怎么样,git的强大自行百度)
(1)在使用git 的同时,应该先学习一些git的常用命令
1.git status
2.git add .
3.git commit -m "commit"
4.git pull origin master
5.git push origin master
(2)安装git ,需要先下载git软件并安装,eclipse集成了git插件
2.照片文件的上传
End
下次再为大家介绍:
- git的使用(相对SVN操作相对较难,也是公司项目使用的主流趋势,不会它你将无法工作,先不说github怎么样,git的强大自行百度)
- 照片文件的上传
- Excel的导入/导出
- 分页查询(使用mybatis-pageHelper 插件)
- mybatis 一对多查询
- mybatis动态块查询
- 接口测试工具postman(一个非常好用的接口测试工具)
- 在项目编码中遇到的问题