1、建立springboot工程
1.環境介紹:
a:jdk版本:1.7html
b:Springboot版本:1.5.6(使用1.5.9的版本整合mybatis會報錯:java.lang.NoClassDefFoundError: org/springframework/dao/support/DaoSupport)java
c:工程類型:maven工程mysql
2.建立maven工程:
2、springboot整合mybatis:
1.添加依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--添加Mybatis依賴 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <!--添加MySQL驅動依賴 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
2.配置application.properties:
#端口設置(默認8080)
server.port=9090
#數據庫相關配置
spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password= spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#mybatis相關配置
mybatis.type-aliases-package=com.example.bean
mybatis.mapper-locations=classpath:mapper/*.xml
3.編寫控制層、業務層代碼:
a:UserController.javaweb
package com.example.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import com.example.service.UserService; @Controller @RequestMapping(value = "/user") public class UserController { @Autowired private UserService userService; @RequestMapping(value = "/selectAll",method=RequestMethod.GET) public ModelAndView selectAll(){ ModelAndView mv = new ModelAndView(); mv = userService.selectAll(); return mv; } }
b:UserService.java(service接口)spring
package com.example.service; import org.springframework.web.servlet.ModelAndView; public interface UserService { public ModelAndView selectAll(); }
c:UserServiceImpl.java(service實現)sql
package com.example.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.servlet.ModelAndView; import com.example.bean.User; import com.example.dao.UserDao; import com.example.service.UserService; @Service(value = "userService") public class UserServiceImpl implements UserService{ @Autowired private UserDao userDao; @Override public ModelAndView selectAll() { ModelAndView mv = new ModelAndView(); List<User> list = userDao.selectAll(); for (User user : list) { System.out.println(user); } mv.addObject("userList", list); mv.setViewName("demo"); return mv; } }
d:UserDao.java(dao層接口)數據庫
package com.example.dao; import java.util.List; import com.example.bean.User; public interface UserDao { public List<User> selectAll(); }
e:UserMapper.xml(mybatis的mapper.xml文件)apache
<?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.example.dao.UserDao"> <resultMap id="BaseResultMap" type="com.example.bean.User" > <id column="pkid" property="pkid" jdbcType="INTEGER" /> <result column="userName" property="userName" jdbcType="VARCHAR" /> <result column="passWord" property="passWord" jdbcType="VARCHAR" /> <result column="userInfo" property="userInfo" jdbcType="VARCHAR" /> </resultMap> <select id="selectAll" resultMap="BaseResultMap"> select * from user where 1=1 </select> </mapper>
f:數據庫表及數據:瀏覽器
CREATE TABLE `user` ( `pkid` int(10) NOT NULL DEFAULT '0', `userName` varchar(20) DEFAULT NULL, `passWord` varchar(20) DEFAULT NULL, `userInfo` varchar(100) DEFAULT NULL, PRIMARY KEY (`pkid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
g:SpringbootApplication.java(springboot啓動類配置)springboot
package com.example; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.example.dao") public class SpringbootMybatisDemoApplication { public static void main(String[] args) { SpringApplication.run(SpringbootMybatisDemoApplication.class, args); } }
3、springboot整合freemark:
1.添加依賴:
<!-- 添加freemark依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
2.添加application.propertis配置:
#freemark配置 spring.freemarker.template-loader-path=classpath:/templates/
spring.freemarker.suffix=.ftl
注:springboot整合freemark後,默認模板的後綴就是.ftl,所以,上面配置文件中的spring.freemarker.suffix=.ftl能夠省略。
3.建立freemark模板文件:
在resources文件夾下建立templates文件夾用於存放.ftl模板文件
demo.ftl文件內容以下:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1>用戶信息表:</h1><hr/> <table> <tr> <td>用戶ID</td> <td>用戶名</td> <td>密碼</td> <td>用戶信息</td> </tr> <#list userList as item> <tr> <td>${item.pkid}</td> <td>${item.userName}</td> <td>${item.passWord}</td> <td>${item.userInfo}</td> </tr> </#list> </table> </body> </html>
4.工程結構圖:
這裏再貼一個完整的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.example</groupId> <artifactId>Springboot_mybatis_demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>Springboot_mybatis_demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.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.7</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--添加Web依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--添加Mybatis依賴 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> <!--添加MySQL驅動依賴 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- 添加freemark依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
至此,Springboot整合mybatis,Springboot整合freemark的全部配置就完成了,啓動springboot項目,瀏覽器輸入127.0.0.1:9090/user/selectAll測試效果以下:
5.效果圖:
4、springboot整合oauth:
還在整合,待續。。。