首先聲明:第一次寫缓醋。如果有什么問(wèn)題的話扔嵌!順著網(wǎng)線過(guò)來(lái)打我啊~~
開(kāi)始吧和泌。
第一步:創(chuàng)建maven項(xiàng)目
對(duì)maven不是很了解的自行百度咯读拆。因?yàn)橹攸c(diǎn)不是maven。
第二步:添加依賴
打開(kāi)pom.xml文件。
添加SpringBoot與MyBatis與MySql的依賴倍阐。
<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.drip</groupId>
<artifactId>Test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath />
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- MYSQL依賴 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- mybatis依賴 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
這個(gè)時(shí)候項(xiàng)目可能會(huì)報(bào)錯(cuò)概疆,因?yàn)橄嚓P(guān)jar包還沒(méi)有引進(jìn)來(lái)。右鍵點(diǎn)擊項(xiàng)目峰搪,選擇Maven→Update Project→OK岔冀。
第三步:新建application.properties配置文件
在在src/main/resources下新建application.properties文件并添加以下內(nèi)容。
#JPA configure
spring.datasource.url = jdbc:mysql://localhost:3306/name
spring.datasource.username = root
spring.datasource.password = 123
spring.datasource.driverClassName = com.mysql.jdbc.Driver
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# DDL mode. This is actually a shortcut for the "hibernate.hbm2ddl.auto" property. Default to "create-drop" when using an embedded database, "none" otherwise.
spring.jpa.hibernate.ddl-auto = update
# Hibernate 4 naming strategy fully qualified name. Not supported with Hibernate 5.
spring.jpa.hibernate.naming.strategy = org.hibernate.cfg.ImprovedNamingStrategy
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
第四步:編寫實(shí)體類
在src/main/java下新建entity包概耻,并新建UserInfo類使套。
package entity;
public class UserInfo {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
字段要與數(shù)據(jù)庫(kù)內(nèi)容同步,如果不一樣鞠柄,請(qǐng)使用:
@Id聲明主鍵侦高。
@Table(name = "表名")。
@Column(name = "字段名")進(jìn)行標(biāo)注厌杜。
第五步:編寫Dao層接口
在src/main/java下新建dao包奉呛,并新建UserDao接口。
package dao;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import entity.UserInfo;
public interface UserDao {
@Select("select * from userinfo where id = #{ID}")
public UserInfo findById(@Param("ID") int id);
}
第六步:編寫啟動(dòng)類
在src/main/java下新建contr包夯尽,并新建Application啟動(dòng)類瞧壮。
package contr;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import dao.UserDao;
import entity.UserInfo;
@SpringBootApplication
@RestController
@MapperScan("dao")
public class Application {
@Autowired
UserDao userDao;
@RequestMapping("/hello")
public String hello(){
UserInfo userInfo = userDao.findById(1);
return "hello:" + userInfo.getName();
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
最關(guān)鍵的在于要使用@MapperScan("dao")進(jìn)行掃描。
而不是使用@ComponentScan匙握。
第七步:創(chuàng)建數(shù)據(jù)庫(kù)
數(shù)據(jù)庫(kù)信息要與application.properties中配置的相同馁痴。
數(shù)據(jù)庫(kù)名,用戶名肺孤,密碼罗晕。
創(chuàng)建userinfo表:
CREATE TABLE `userinfo` (
`name` varchar(255) NOT NULL,
`id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
自己在表里隨意添加一條 id=1,name=小明的數(shù)據(jù)赠堵,用于測(cè)試小渊。
整體:
最后一步:瀏覽效果
啟動(dòng)main方法。
在瀏覽器中輸入: localhost:8080/hello 可以看到如下效果: