初次搭建ssm(springboot+springMvc+Mybatis)框架实践之路
程序员文章站
2022-07-12 22:30:20
...
spring,都说是程序员的春天到了,所以,作为测试工程师的我也搭建了个ssm工程。以前刚入IT行时,人家一谈框架,我就觉得人家很厉害,自己也很少去研究框架,总感觉无从下手,各种配置千丝万缕,不胜其烦。所谓工欲善其事必先利其器,一个高效又结构优雅的框架确实令人赏心悦目。配置简单,结构优雅正是spring boot所擅长的。我在网上收罗了一大堆博客,五花八门,也查看了spring 官网的文档,但是都是一些片段性的guides,对于一个如此庞大的框架,一时我迷失了方向,于是上Google,看YouTube上老外的教程视频,对信息的筛选太重要了。为那些曾经的忐忑和踩过的坑,梳理了一遍搭建流程,写就此文。
- 在intellij idea 中创建—>工程——>Spring Initializr,设置如下,点击下一步
- 配置工程名元数据,设置工程groupid,artifactId,我这里用maven构建,所以type选mavenproject,工程打包方式,需要部署到web容器的话,我这里选war包,最后设置编译工程的jdk版本。
- 选定springboot版本,设置工程依赖的模块,我这里选了web和mybatis,从而能集成springboot mvc mybatis,点击下一步,点击完成。
- 工程构建完成,初始结构如下:
- 分层设计,结构化你的代码,官网推荐如下的工程目录结构
可以参考springboot的参考文档,调整后的目录结构如下:
- 相应的pom文件如下:
<?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>springboot-mybatis-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>springboot-mybatis-demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.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.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <executions> <execution> <id>Generate MyBatis Artifacts</id> <goals> <goal>generate</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
-
pom文件中配置了MyBatis Generator插件(俗称MBG)来自动生成DAO层代码,包括model,mapper等文件,很多配置细节,可以参考官方文档,插件配置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"> <!--http://www.mybatis.org/generator/configreference/xmlconfig.html#--> <generatorConfiguration> <properties resource="application.properties"/> <classPathEntry location="D:\repo\mysql\mysql-connector-java\5.1.44\mysql-connector-java-5.1.44.jar"/> <context id="MySqlTables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true"/> </commentGenerator> <jdbcConnection driverClass="${spring.datasource.driverClassName}" connectionURL="${spring.datasource.url}" userId="${spring.datasource.username}" password="${spring.datasource.password}"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <javaModelGenerator targetPackage="com.example.demo.entity" targetProject="src\main\java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <sqlMapGenerator targetPackage="mybatis.mapper" targetProject="src\main\resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.mapper" targetProject="src\main\java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <table schema="pop_qa" tableName="users" domainObjectName="User" enableCountByExample="false" enableSelectByExample="false" enableUpdateByExample="false" selectByExampleQueryId="false" enableSelectByPrimaryKey="false" enableUpdateByPrimaryKey="false" enableDeleteByPrimaryKey="false" selectByPrimaryKeyQueryId="false" enableDeleteByExample="false"> <property name="useActualColumnNames" value="true"/> <!--<generatedKey column="ID" sqlStatement="DB2" identity="true"/>--> <!--<columnOverride column="DATE_FIELD" property="startDate"/>--> <ignoreColumn column="FRED"/> <columnOverride column="user_sex" javaType="com.example.demo.enums.UserSexEnum"/> </table> </context> </generatorConfiguration>
-
application.properties配置如下,主要是mybatis配置文件路径、mapper.xml存放路径,需要映射的实体类所在包路径,以及数据连接配置。
mybatis.config-locations=classpath:mybatis/mybatis-config.xml mybatis.mapper-locations=classpath:mybatis/mapper/*.xml mybatis.type-aliases-package=com.example.demo.entity spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://192.168.195.161:3306/pop_qa?useUnicode=true&characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password=root
-
最后附上建表sql
CREATE TABLE `users` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键id', `userName` varchar(32) DEFAULT NULL COMMENT '用户名', `passWord` varchar(32) DEFAULT NULL COMMENT '密码', `user_sex` varchar(32) DEFAULT NULL, `nick_name` varchar(32) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=utf8
实例工程已开源https://github.com/WenRui13/springboot-mybatis-demo
参考:
上一篇: 从零开始学习搭建mybatis框架
下一篇: Mybatis - 缓存