标签:-- 根据 tab roo r.java 成功 install figure cti
1.在worksapce下,dos窗口输入spring init -g=com.briup.apps -a=poll3 -d=mysql,mybatis,web poll3
2.下载依赖,cd到poll3,执行mvn install,会报错,因为没有配置数据库的初始数据
解决方法:eclipse导入poll3,打开/resources/application.properties,输入
spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/pro2.0 spring.datasource.username=root spring.datasource.password=root mybatis.mapper-locations= classpath:/mapper/**/*.xml
mybatis.mapper-locations=classpath:/mapper/**/*.xml 这个配置是为了让dao层找到mapper.xml文件的资源位置,和mapper文件夹下的mapper.xml映射
3.修改pox.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.braup.apps</groupId> <artifactId>poll</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.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-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> <!-- swagger API文档 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</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> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
4.三层架构
5.src/main/resources下新建mapper文件夹,用来保存mapper.xml文件
6.在config包下创建MybatisConfig.java,用来配置mybatis
package com.briup.apps.poll.config; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Configuration; @Configuration @MapperScan("com.briup.apps.poll") public class MybatisConfig { }
本来应该在DemoApplication.java中配置,已进行标记
package com.braup.apps.poll; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.braup.apps.poll.dao") public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
7.进行测试,mvn spring-boot:run,启动成功表明热部署,三层架构,swagger部署成功
8.编写数据库访问层代码
8.1.手写(时间长,费劲,代码冗余)
8. 2.使用mybatis generator(自动生成bean和dao以及mapper.xml文件)。
mybatis不是框架,是一个插件,不属于项目的一部分,产生的代码是项目的一部分。
8.3配置mabatis generator框架
8.3.1 pom.xml添加依赖
<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> </plugin>
8.3.2添加配置文件src/main/resources下添加------------1.根据那些表产生bean. 2.产生的pojo放在哪里. 3.产生的mapper接口放在哪里 4.产生的mapper实现类放在哪里 ........
<?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"> <!-- mybatis-generator的核心配置文件 --> <generatorConfiguration> <classPathEntry location="/Users/lichunyu/springboot/repository/mysql/mysql-connector-java/5.1.45/mysql-connector-java-5.1.45.jar" /> <context id="DB2Tables" targetRuntime="MyBatis3"> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/poll2.0" userId="root" password="root"> </jdbcConnection> <!--指定生成的类型为java类型,避免数据库中number等类型字段 --> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!--自动生成的实体的存放包路径 --> <javaModelGenerator targetPackage="com.briup.apps.poll.bean" targetProject="./src/main/java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!--自动生成的*Mapper.xml文件存放路径 --> <sqlMapGenerator targetPackage="mapper" targetProject="./src/main/resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!--自动生成的*Mapper.java存放路径 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.briup.apps.poll.dao" targetProject="./src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 映射配置 --> <table tableName="poll_survy" domainObjectName="Survy" ></table> <!-- <table tableName="poll_school" domainObjectName="School" ></table> <table tableName="poll_grade" domainObjectName="Grade" ></table> <table tableName="poll_clazz" domainObjectName="Clazz" ></table> <table tableName="poll_course" domainObjectName="Course" ></table> <table tableName="poll_grade_course" domainObjectName="GradeCourse" ></table> <table tableName="poll_user" domainObjectName="User" ></table> <table tableName="poll_option" domainObjectName="Option" ></table> <table tableName="poll_question" domainObjectName="Question" ></table> <table tableName="poll_questionnaire" domainObjectName="Questionnaire" ></table> <table tableName="poll_questionnaire_question" domainObjectName="QuestionnaireQuestion" ></table> --> </context> </generatorConfiguration>
8.3.3开始工作-( 要关闭项目)
mvn -Dmybatis.generator.overwrite=true mybatis-generator:generate
maven项目重构-使用了mybatis generator插件
标签:-- 根据 tab roo r.java 成功 install figure cti
原文地址:https://www.cnblogs.com/zzuli/p/9226481.html