1. 程式人生 > >初學springboot、springMVC、Mybatis專案--學習筆記

初學springboot、springMVC、Mybatis專案--學習筆記

1.建立sts專案 建立步驟連結:https://blog.csdn.net/he90227/article/details/53308222 (我在建立專案時,選擇了mysql,但是初始測試時,未配置資料庫連結,專案啟動報錯:Failed to configure a DataSource: ‘url’ attribute is not specified and no embedde,解決方法如圖:在這裡插入圖片描述

注意:在使用Autowired自動注入時,需要將紅色框體中的內容刪除,否則報錯,Property ‘sqlSessionFactory’ or ‘sqlSessionTemplate’ are required

建立config包及Application檔案:

package com.fengsx;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import com.fengsx.config.ApplicationConfig;

/**
 * 	Configuration配置spring並啟動spring容器
 * 	Import--ApplicationConfig應用配置類
 * @author xi
 *
 */
@Configuration
@SpringBootApplication
@Import({ApplicationConfig.class})
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}

ApplicationConfig檔案:

package com.fengsx.config;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.ComponentScan;

/**
 * 	應用配置類
 * 	MapperScan(掃描mapper檔案)
 * 	ComponentScan告訴Spring 哪個packages 的用註解標識的類 會被spring自動掃描並且裝入bean容器
 * 		例如,如果你有個類用@Controller註解標識了,那麼,如果不加上@ComponentScan,自動掃描該controller,
 * 		那麼該Controller就不會被spring掃描到,更不會裝入spring容器中,因此你配置的這個Controller也沒有意義。
 * 	微服務 通過EnableFeignClients呼叫其他服務的api(這裡未使用)
 * @author xi
 *
 */
@MapperScan("com.fengsx.domain.persistence")
@ComponentScan({"com.fengsx.api.controller",
		"com.fengsx.domain.service"})
public class ApplicationConfig {
	
}

5.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.fengsx</groupId>
	<artifactId>demofengsx</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>demofengsx</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.5.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-data-rest</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.2</version>
		</dependency>
		
		<dependency>
		    <groupId>commons-logging</groupId>
		    <artifactId>commons-logging</artifactId>
		    <version>1.2</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.46</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<profiles>
		<profile>
			<id>develop</id>
			<properties>
				<profiles.active>develop</profiles.active>
			</properties>
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
		</profile>
	</profiles>
	<build>
		<!-- 載入config中的application.properties -->
		<finalName>${project.artifactId}-${version}</finalName>
		<resources>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
				<excludes>
					<exclude>config/</exclude>
					<exclude>static/assets/**</exclude>
				</excludes>
			</resource>
			<resource>
				<directory>src/main/resources/config/${profiles.active}</directory>
				<targetPath>.</targetPath>
				<filtering>true</filtering>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
				<includes>
					<include>static/assets/**</include>
				</includes>
			</resource>
		</resources>
		<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>
				<dependencies>
			        <dependency>
			            <groupId>mysql</groupId>
			            <artifactId>mysql-connector-java</artifactId>
			            <version>5.1.46</version>
			        </dependency>
				</dependencies>
			</plugin>
		</plugins>
	</build>


</project>

6.application.properties簡單配置如下:

# --- {Logging}
#logging.level.DEBUG
logging.level.org.springframework.web=INFO
logging.level.com.netflix.discovery=DEBUG
logging.level.org.hibernate=ERROR
logging.level.org.springframework.boot.autoconfigure.security=WARN
logging.level.com.fengsx.domain.persistence=DEBUG

# --- {MyBatis}
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/cf_couture?characterEncoding=UTF-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123

mybatis.type-aliases-package=com.fengsx.domain

7.generatorConfig.xml自動生成modal、mapper.java、mapper.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">
<!-- Reference: http://www.mybatis.org/generator/index.html -->
<generatorConfiguration>
    <context id="MySQLTables" targetRuntime="MyBatis3">
    
        <!--===============-->
        <!-- 生成共通屬性配置 -->
        <!--===============-->
        <commentGenerator>
            <property name="suppressDate" value="true" />
            <property name="suppressAllComments" value="true" />
        </commentGenerator>

        <!--===============-->
        <!-- MySQL資料來源配置 -->
        <!--===============-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://127.0.0.1:3306/cf_couture?characterEncoding=UTF-8"
            userId="root" password="123" />

        <javaModelGenerator targetPackage="com.fengsx.domain.model"
            targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="com.fengsx.domain.persistence"
            targetProject="src/main/resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <javaClientGenerator targetPackage="com.fengsx.domain.persistence"
            targetProject="src/main/java" type="XMLMAPPER">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!--===============-->
        <!-- 生成的業務表配置 -->
        <!--===============-->
        <table tableName="t_edit_book" domainObjectName="EditBook"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false">
            <property name="useActualColumnNames" value="false" />
        </table>
    </context>
</generatorConfiguration>