springBoot+Mybatis整合小案例《一》
2018-01-15 11:30
661 查看
最近,突然对springBoot感兴趣,看了网上的各种大牛的教程,写出了几个完整的小Demo,因为自己也是初学者,所以将自己其中的一个小案例的配置过程来写一下,希望对和我一样的初学者能提供一点参考(不足之处,还请指教)。
一:准备工作
我的IDE是STS是3.6.3版本。
新建一个maven项目,在pom.xml中添加以下内容:
<parent >
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-starter-parent </artifactId>
<version> 1.3.0.RELEASE </version>
</parent >
<!-- 添加 web 应用的依赖 -->
<dependencies >
<dependency>
<groupId> org.springframework.boot </groupId >
<artifactId> spring-boot-starter-web </artifactId>
</dependency>
</dependencies >添加完成以后,我们会看到maven中自己给我们下载了很多的依赖
之所以会下载这么多的依赖,是因为我们在pom.xml中添加的那些依赖,
这就说明我们要使用 Spring搭建一个web 项目,SpringBoot就 自己 的帮我们添加了它认为的 一个web 应用所需要的依赖。正是因为这个SpringBoot让我们减少了很多 pom.xml配置的麻烦。并且自动整合了 SpringMvc和Spring 。这些依赖中居然还包括 tomcat,说明我们不需要自己部署tomcat,因为tomcat已经内嵌在我们的应用中了。
先将我的目录结构展示出来
首先,我们先写一个HelloWord让项目先跑起来
@RestController
@EnableAutoConfiguration
@SpringBootApplication
public class Application {
@RequestMapping("/")
public String hello(){
return "Hello SpringBoot~~~";
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}启动后控制台会出现如下信息
然后我们在浏览器中查看
OK,已经成功了。
下面我们在来整合SSM。按照上面的目录先创建它的controller,dao,domain类(为了简便,没有写service),首先在它的pom.xml中添加完整的信息
<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.jsb</groupId>
<artifactId>SSM</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent >
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-starter-parent </artifactId>
<version> 1.3.0.RELEASE </version>
</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>
<!-- 添加 web 应用的依赖 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<!-- 把项目打成jar包运行 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>添加完成后我们需要配置它的application.properties文件,在这里我使用的mysql数据库
Controller层代码
实体类代码
配置UserMapper.xml映射文件<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.jsb.dao.UserMapper" >
<select id="selectUserByName" resultType="User">
SELECT * FROM user WHERE username = #{username}
</select>
</mapper>application.properties的完整配置spring.datasource.url=jdbc:mysql://localhost:3306/springboot spring.datasource.username=wzdsg spring.datasource.password=1234 spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.jpa.database = mysql
mybatis.mapper-locations=classpath*:mapper/*.xml
mybatis.type-aliases-package=com.jsb.domain
数据库信息
启动类Application代码
package com;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* @Title: Application.java
* @Package com
* @Description: SSM整合的启动入口
* @author twelve
* @date 2018年1月15日 上午9:33:24
*
* @version V1.0
*/
@SpringBootApplication
@EnableTransactionManagement// 开启事务
@ConfigurationProperties(prefix="spring.datasource")
@MapperScan("com.jsb.dao")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
注意:将springBoot的启动类放至在包外,不然会扫描不到。
然后启动项目,在地址栏输入http://localhost:8080/wzdsg,发现
Yes,完成!
因我也是初学者,不足之处,还请各位大神指明。
另,附上一位大牛的博客地址田守枝Java技术博客
一:准备工作
我的IDE是STS是3.6.3版本。
新建一个maven项目,在pom.xml中添加以下内容:
<parent >
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-starter-parent </artifactId>
<version> 1.3.0.RELEASE </version>
</parent >
<!-- 添加 web 应用的依赖 -->
<dependencies >
<dependency>
<groupId> org.springframework.boot </groupId >
<artifactId> spring-boot-starter-web </artifactId>
</dependency>
</dependencies >添加完成以后,我们会看到maven中自己给我们下载了很多的依赖
之所以会下载这么多的依赖,是因为我们在pom.xml中添加的那些依赖,
这就说明我们要使用 Spring搭建一个web 项目,SpringBoot就 自己 的帮我们添加了它认为的 一个web 应用所需要的依赖。正是因为这个SpringBoot让我们减少了很多 pom.xml配置的麻烦。并且自动整合了 SpringMvc和Spring 。这些依赖中居然还包括 tomcat,说明我们不需要自己部署tomcat,因为tomcat已经内嵌在我们的应用中了。
先将我的目录结构展示出来
首先,我们先写一个HelloWord让项目先跑起来
@RestController
@EnableAutoConfiguration
@SpringBootApplication
public class Application {
@RequestMapping("/")
public String hello(){
return "Hello SpringBoot~~~";
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}启动后控制台会出现如下信息
然后我们在浏览器中查看
OK,已经成功了。
下面我们在来整合SSM。按照上面的目录先创建它的controller,dao,domain类(为了简便,没有写service),首先在它的pom.xml中添加完整的信息
<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.jsb</groupId>
<artifactId>SSM</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent >
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-starter-parent </artifactId>
<version> 1.3.0.RELEASE </version>
</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>
<!-- 添加 web 应用的依赖 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<!-- 把项目打成jar包运行 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>添加完成后我们需要配置它的application.properties文件,在这里我使用的mysql数据库
spring.datasource.url=jdbc:mysql://localhost:3306/springboot spring.datasource.username=wzdsg spring.datasource.password=1234 spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.jpa.database = mysqlDAO层代码
package com.jsb.dao; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; import com.jsb.domain.User; /** * @Title: UserMapper.java * @Package com.jsb.dao * @Description: mapper接口 * @author twelve * @date 2018年1月15日 上午9:49:00 * * @version V1.0 */ @Mapper @Repository public interface UserMapper { User selectUserByName(String username); }
Controller层代码
package com.jsb.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.Res 4000 ponseBody; import org.springframework.web.bind.annotation.RestController; import com.jsb.dao.UserMapper; import com.jsb.domain.User; /** * @Title: UserController.java * @Package com.jsb.controller * @Description: spingBoot的controller层 * @author twelve * @date 2018年1月15日 上午9:56:09 * * @version V1.0 */ @RestController public class UserController { // 依赖注入 @Autowired private UserMapper userMapper; @RequestMapping("/wzdsg") public User user(){ // 调用DAO层 User user = userMapper.selectUserByName("王大帅比"); return user; } }
实体类代码
package com.jsb.domain; /** * @Title: User.java * @Package com.jsb.domain * @Description: springBoot的实体类 * @author twelve * @date 2018年1月15日 上午9:46:23 * * @version V1.0 */ public class User { private int id; private String username; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "User [id=" + id + "," + " username=" + username + ", age=" + age + "]"; } }
配置UserMapper.xml映射文件<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.jsb.dao.UserMapper" >
<select id="selectUserByName" resultType="User">
SELECT * FROM user WHERE username = #{username}
</select>
</mapper>application.properties的完整配置spring.datasource.url=jdbc:mysql://localhost:3306/springboot spring.datasource.username=wzdsg spring.datasource.password=1234 spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.jpa.database = mysql
mybatis.mapper-locations=classpath*:mapper/*.xml
mybatis.type-aliases-package=com.jsb.domain
数据库信息
启动类Application代码
package com;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* @Title: Application.java
* @Package com
* @Description: SSM整合的启动入口
* @author twelve
* @date 2018年1月15日 上午9:33:24
*
* @version V1.0
*/
@SpringBootApplication
@EnableTransactionManagement// 开启事务
@ConfigurationProperties(prefix="spring.datasource")
@MapperScan("com.jsb.dao")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
注意:将springBoot的启动类放至在包外,不然会扫描不到。
然后启动项目,在地址栏输入http://localhost:8080/wzdsg,发现
Yes,完成!
因我也是初学者,不足之处,还请各位大神指明。
另,附上一位大牛的博客地址田守枝Java技术博客
相关文章推荐
- springboot + mybatis +easyUI整合案例
- Spring boot入门,整合mybatis开发案例
- Springboot 整合 Mybatis 的完整 Web 案例
- Springboot 整合 Mybatis 的完整 Web 案例
- Springboot 整合 Mybatis 的完整 Web 案例
- Springboot 整合 Mybatis 的完整 Web 案例
- Spring Boot + MyBatis 整合 案例
- Spring Boot + MyBatis + Redis整合小案例
- Springboot 整合 Mybatis 的完整 Web 案例
- Springboot 整合 Mybatis 的完整 Web 案例
- Spring Boot整合MyBatis,附带单元测试
- SpringBoot整合MyBatis及Thymeleaf
- springboot 整合 Mybatis (纯配置文件)
- Spring boot整合mybatis
- 整合springboot-mybatis提供RESTful风格SaaS服务
- springboot整合mybatis
- SpringBoot整合Mybatis出现的错误
- java鬼混笔记:springboot 6、springboot整合mybatis(支持多数源)
- Spring Boot 整合 Mybatis Annotation 注解的完整 Web 案例
- Spring boot 整合 mybatis + druid