1.在worksapce下,dos窗口輸入spring init -g=com.briup.apps -a=poll3 -d=mysql,mybatis,web poll3java
2.下載依賴,cd到poll3,執行mvn install,會報錯,由於沒有配置數據庫的初始數據mysql
解決方法:eclipse導入poll3,打開/resources/application.properties,輸入web
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映射spring
3.修改pox.xml,內容以下sql
<?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文件apache
6.在config包下建立MybatisConfig.java,用來配置mybatisspringboot
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中配置,已進行標記mybatis
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