發現靜態資源可以訪問,並且默認statci路徑
package com.tzy.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
/**
* Created by 70416 on 2018/4/10.
*/
@ComponentScan("com.tzy.springbootH")
//表示開始注入spring容器,創建Tomcat,srping在加載
@EnableAutoConfiguration
public class APP {
public static void main(String[] args) {
//主函數的啓動入口,springboot是通過應用程序運行的
SpringApplication.run(APP.class,args);
}
}
package com.tzy.springbootH;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by 70416 on 2018/4/10.
*/
@RestController
public class IndexController {
@RequestMapping("/indexc")
public String index(){
System.out.println(5/0);
return "indexc";
}
}
做了一個數學異常,攔截
package com.tzy.springbootH; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.HashMap; import java.util.Map; /** * Created by 70416 on 2018/4/10. */ //全局捕獲異常類 @ControllerAdvice public class GlobalExceptionHandler { //如果是返回json格式@ResponseBody 如果是返回頁面,返回String類型 //類型結果制定404頁面 @ExceptionHandler(RuntimeException.class) @ResponseBody public Map<String,Object> resultError(){ Map<String,Object> result = new HashMap<String,Object>(); result.put("errorCode","500"); result.put("errorMsg","系統錯誤!!"); return result; } }
原理其實就是spring的異常通知!
Spring整合頁面
freemarker,Velocity,thymeleaf就是動態頁面靜態化的模板引擎
pom加入引用
<?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.tzy.app</groupId>
<artifactId>springboot_helloworld</artifactId>
<version>1.0-SNAPSHOT</version>
<!--引入springBoot父類依賴-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<dependencies>
<!--springboot-wed組件 springmvc+spring+mybatis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--引入freeMarker的依賴包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
</dependencies>
</project>
默認路徑
templates
建立freemarker
this is HelloWorld<br> ${name}<br> <br><br> <#if sex==1> 男 <#elseif sex==2> 女 <#else> 其他 </#if> <br> <#list userlist as user> ${user}<br> </#list>
package com.tzy.springbootH; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; import java.util.List; import java.util.Map; /** * Created by 70416 on 2018/4/10. */ @Controller public class IndexController { @RequestMapping("/index") public String indexSY(Map<String,Object> result){ System.out.println("IndexController...index"); result.put("name","zhangsna"); result.put("sex",1); List<String> list = new ArrayList<String>(); list.add("lisi"); list.add("wangwu"); result.put("userlist",list); return "index"; } }
application.properties
# FREEMARKER (FreeMarkerAutoConfiguration)
spring.freemarker.allow-request-override=false
spring.freemarker.cache=true
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
spring.freemarker.prefix=
spring.freemarker.request-context-attribute=
spring.freemarker.settings.*=
spring.freemarker.suffix=.ftl
spring.freemarker.template-loader-path=classpath:/templates/ # comma-separated list
spring.freemarker.view-names= # whitelist of view names that can be resolved
spring boot整合jsp需要war類型的工程,加入jsp依賴
springboot整合jdbcTemplate
<!--整合jdbcTemplate-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for test
-- ----------------------------
DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nums` int(11) NOT NULL,
`name` varchar(45) COLLATE utf8_bin DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_numbs` (`nums`)
) ENGINE=InnoDB AUTO_INCREMENT=4229 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
-- ----------------------------
-- Records of test
-- ----------------------------
INSERT INTO `test` VALUES ('1', '1', '2');
INSERT INTO `test` VALUES ('2', '12', 'name-test');
INSERT INTO `test` VALUES ('3', '12', 'name-test');
INSERT INTO `test` VALUES ('4', '12', 'A');
INSERT INTO `test` VALUES ('5', '12', 'A');
INSERT INTO `test` VALUES ('6', '12', 'A');
INSERT INTO `test` VALUES ('7', '12', 'A');
INSERT INTO `test` VALUES ('8', '12', 'A');
INSERT INTO `test` VALUES ('9', '12', 'A');
INSERT INTO `test` VALUES ('10', '12', '0');
INSERT INTO `test` VALUES ('11', '12', '0');
INSERT INTO `test` VALUES ('12', '0', '0');
INSERT INTO `test` VALUES ('13', '1', '0');
INSERT INTO `test` VALUES ('14', '2', '0');
INSERT INTO `test` VALUES ('15', '3', '0');
INSERT INTO `test` VALUES ('16', '4', '0');
INSERT INTO `test` VALUES ('17', '5', '0');
INSERT INTO `test` VALUES ('18', '6', '0');
INSERT INTO `test` VALUES ('19', '7', '0');
INSERT INTO `test` VALUES ('20', '8', '0');
INSERT INTO `test` VALUES ('21', '9', '0');
INSERT INTO `test` VALUES ('22', '0', '0');
INSERT INTO `test` VALUES ('23', '1', '0');
INSERT INTO `test` VALUES ('24', '2', '0');
INSERT INTO `test` VALUES ('25', '3', '0');
INSERT INTO `test` VALUES ('26', '4', '0');
INSERT INTO `test` VALUES ('27', '5', '0');
INSERT INTO `test` VALUES ('28', '6', '0');
INSERT INTO `test` VALUES ('29', '7', '0');
INSERT INTO `test` VALUES ('30', '8', '0');
spring.datasource.url=jdbc:mysql://localhost:3306/gp spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver
package com.tzy.serivice; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; /** * Created by 70416 on 2018/4/10. */ @Service public class UserService { @Autowired private JdbcTemplate jdbcTemplate; public void createUser(String name,Integer nums){ System.out.println("createUser"); jdbcTemplate.update("INSERT INTO test VALUES (null,?,?);",nums,name); System.out.println("創建用戶成功"); } }
package com.tzy.controller; import com.tzy.serivice.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Created by 70416 on 2018/4/10. */ @RestController public class UserController { @Autowired private UserService userService; @RequestMapping("/createUser") public String createUser(String name, Integer nums) { userService.createUser(name, nums); return "seccess"; } }
重點APP換多包掃描
package com.tzy.app; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ComponentScan; /** * Created by 70416 on 2018/4/10. */ @ComponentScan(basePackages = {"com.tzy.springbootH","com.tzy.serivice","com.tzy.controller"}) //表示開始注入spring容器,創建Tomcat,srping在加載 @EnableAutoConfiguration public class APP { public static void main(String[] args) { //主函數的啓動入口,springboot是通過應用程序運行的 SpringApplication.run(APP.class,args); } }
成功了
JPA整合
加入依賴
<!--JPA數據源-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
使用之前加入的數據源配置寫一個實現類POJO
package com.tzy.entity;
import javax.annotation.Generated;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
* Created by 70416 on 2018/4/10.
*/
//註解對應數據庫表
@Entity(name="test")
public class User {
@Id
@GeneratedValue
private Integer id;
@Column
private String name;
@Column
private Integer nums;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getNums() {
return nums;
}
public void setNums(Integer nums) {
this.nums = nums;
}
}
dao
package com.tzy.dao;
import com.tzy.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* Created by 70416 on 2018/4/10.
*/
//繼承這個就可以實現所有的怎刪改查 這就是JPA
public interface UserDao extends JpaRepository<User,Integer> {
}
service添加JPA方法,注意看區別
package com.tzy.serivice;
import com.tzy.dao.UserDao;
import com.tzy.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
/**
* Created by 70416 on 2018/4/10.
*/
@Service
public class UserService {
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
private UserDao userDao;
public void createUser(String name,Integer nums){
System.out.println("createUser");
jdbcTemplate.update("INSERT INTO test VALUES (null,?,?);",nums,name);
System.out.println("創建用戶成功");
}
public User getJpaUser(Integer id){
System.out.println("getUser");
User one = userDao.findOne(id);
System.out.println("查找用戶成功");
return one;
}
}
Controller
package com.tzy.controller;
import com.tzy.entity.User;
import com.tzy.serivice.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by 70416 on 2018/4/10.
*/
@RestController
public class UserController {
@Autowired
private UserService userService;
//jdbcTemplate實現增加
@RequestMapping("/createUser")
public String createUser(String name, Integer nums) {
userService.createUser(name, nums);
return "seccess";
}
//jpa實現查找
@RequestMapping("/getUser")
public User getUser(Integer id){
return userService.getJpaUser(id);
}
}
package com.tzy.app; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.orm.jpa.EntityScan; import org.springframework.context.annotation.ComponentScan; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; /** * Created by 70416 on 2018/4/10. */ @ComponentScan(basePackages = {"com.tzy.springbootH","com.tzy.serivice","com.tzy.controller"}) @EnableJpaRepositories("com.tzy.dao") @EntityScan("com.tzy.entity") //表示開始注入spring容器,創建Tomcat,srping在加載 @EnableAutoConfiguration public class APP { public static void main(String[] args) { //主函數的啓動入口,springboot是通過應用程序運行的 SpringApplication.run(APP.class,args); } }
*******************spring整合mybatis******************
POJO
package com.tzy.entity; /** * Created by 70416 on 2018/4/10. */ public class User { private Integer id; private String name; private Integer nums; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getNums() { return nums; } public void setNums(Integer nums) { this.nums = nums; } }
Mapper
package com.tzy.mapper; import com.tzy.entity.User; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; /** * Created by 70416 on 2018/4/10. */ public interface UserMapper { @Select("SELECT * FROM test WHERE NAME = #{name}") User findByName(@Param("name") String name); @Insert("INSERT INTO test VALUES(NULL,#{nums},#{name})") int insert(@Param("name") String name,@Param("nums")Integer nums); }
Service
package com.tzy.serivice; import com.tzy.entity.User; import com.tzy.mapper.UserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * Created by 70416 on 2018/4/10. */ @Service public class UserService { @Autowired private UserMapper userMapper; public User findByName(String name){ return userMapper.findByName(name); } public int insert(String name,Integer nums){ return userMapper.insert(name, nums); } }
Controller
package com.tzy.controller; import com.tzy.entity.User; import com.tzy.serivice.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Created by 70416 on 2018/4/10. */ @RestController public class UserController { @Autowired private UserService userService; @RequestMapping("/findByName") public User findByName(String name){ return userService.findByName(name); } @RequestMapping("/insert") public String insert(String name,Integer nums){ int insert = userService.insert(name, nums); if(insert>0){ return "seccess"; } return "error"; } }
啓動主入口
package com.tzy.app; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.orm.jpa.EntityScan; import org.springframework.context.annotation.ComponentScan; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; /** * Created by 70416 on 2018/4/10. */ @ComponentScan(basePackages = {"com.tzy.springbootH","com.tzy.serivice","com.tzy.controller"}) @MapperScan(basePackages = {"com.tzy.mapper"}) //表示開始注入spring容器,創建Tomcat,srping在加載 @EnableAutoConfiguration public class APP { public static void main(String[] args) { //主函數的啓動入口,springboot是通過應用程序運行的 SpringApplication.run(APP.class,args); } }