<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.17.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
@RestController
@SpringBootApplication
public class Example {
@RequestMapping("/")
String home() {
return "Hello World!";
}
@RequestMapping("/mp")
HashMap home1() {
HashMap hm = new HashMap();
hm.put("id", 99);
return hm;
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Example.class, args);
}
}
@Controller
@SpringBootApplication
public class Example {
@Autowired
private User user;
@ResponseBody
@RequestMapping("/")
String home() {
return "Hello World!";
}
@ResponseBody
@GetMapping("/mp")
HashMap home1() {
HashMap hm = new HashMap();
hm.put("id", 99);
return hm;
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Example.class, args);
}
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
Spring Boot 优于Spring mvc ,SSM,SSH 的一个亮点就是他使用了好多的注解。
这个注解的作用是将其他的类,接口引入,类似于之前的类的初始化等,用这个注解,类中或接口的方法就可以直接调用了。
这个注解和@Inject,@Resource 作用类似,都能注入类, 接口.
这个注解的作用是告诉Servlet 这个类是一个控制器,当前台调用后台的时候,根据名称就能找到这个控制类,然后去执行里面的方法。他类似于Spring mvc 中的@Controller,他继承自@Controller。
这个注解的作用是当前台界面调用Controller处理数据时候告诉控制器怎么操作。get 对应查询,put 对应修改,post 对应增加, delete 对应删除。
这四个注解都是用来传参数的,第一个是用来传递http://localhost:8080/page1?id=1 这种用的。第二个和第三个用来处理http://localhost:8080/page1/1这种传参数的,后面这个是用来传对象用的。
spring.redis.host = 192.168.0.246
spring.datasource.url=jdbc:mysql://localhost/accounts
spring.datasource.username=root
spring.datasource.password=ps123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.stat-view-servlet.login-username=zjh
spring.datasource.druid.stat-view-servlet.login-password=zjh123456
spring.datasource.druid.filter.stat.slow-sql-millis=2000
@RestController
public class RedisController {
@Autowired
StringRedisTemplate srt;
@GetMapping("/redis")
public String redis() {
srt.boundValueOps("mysex").set("男");
return srt.boundValueOps("mysex").get();
}
}
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
@RestController
public class JdbcController {
@Autowired
JdbcTemplate jt;
@GetMapping("/jdbc")
public List<Map<String, Object>> redis() {
List<Map<String, Object>> query = jt.query("select * from emp_t", new ColumnMapRowMapper());
return query;
}
}
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
/**
* 可以理解为这个类是spring的配置文件
* <bean id ="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
*
*@author
*@date 2018年11月27日下午8:18:36
*
*/
@Configuration
public class ConfigBin {
@Value("${spring.datasource.driver-class-name}")
private String className;
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String usname;
@Value("${spring.datasource.password}")
private String pass;
@Bean
public DataSource dataSource() {
DruidDataSource dt = new DruidDataSource();
dt.setUrl(url);
dt.setDriverClassName(className);
dt.setPassword(pass);
dt.setUsername(usname);
return dt;
}
}
druid 手动监听sql语句 将下列代码放置ConfigBin类中,只监控服务产生到结束
设置阀值 spring.datasource.druid.filter.stat.slow-sql-millis=2000
手动引入jar包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
@Bean
public ServletRegistrationBean statViewServlet(){
//创建servlet注册实体
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
//设置ip白名单
servletRegistrationBean.addInitParameter("allow","127.0.0.1");
//设置ip黑名单,如果allow与deny共同存在时,deny优先于allow
servletRegistrationBean.addInitParameter("deny","192.168.0.19");
//设置控制台管理用户
servletRegistrationBean.addInitParameter("loginUsername","druid");
servletRegistrationBean.addInitParameter("loginPassword","123456");
//是否可以重置数据
servletRegistrationBean.addInitParameter("resetEnable","false");
return servletRegistrationBean;
}
@Bean
public FilterRegistrationBean statFilter(){
//创建过滤器
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
//设置过滤器过滤路径
filterRegistrationBean.addUrlPatterns("/*");
//忽略过滤的形式
filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
return filterRegistrationBean;
}
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>