Skip to content

Commit

Permalink
springboot小练习
Browse files Browse the repository at this point in the history
  • Loading branch information
Elijah-Guo committed Aug 25, 2018
1 parent e80f83b commit 1860372
Show file tree
Hide file tree
Showing 45 changed files with 1,008 additions and 0 deletions.
89 changes: 89 additions & 0 deletions springbootdemo/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?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.itheima</groupId>
<artifactId>springbootdemo</artifactId>
<version>1.0-SNAPSHOT</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- springBoot JPA的起步依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- MySQL连接驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--freemarker-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!--mybatis起步依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>

<!--测试的起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<!-- 配置使用redis启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
</dependencies>

<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>


</project>
171 changes: 171 additions & 0 deletions springbootdemo/springbootdemo.iml

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions springbootdemo/src/main/java/com/itheima/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.itheima;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.itheima.mapper")
public class Application {

public static void main(String[] args) {

SpringApplication.run(Application.class,args);
}
}
62 changes: 62 additions & 0 deletions springbootdemo/src/main/java/com/itheima/Entity/UserEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.itheima.Entity;

import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import javax.persistence.*;

@Document(indexName = "springboot",type = "haha")
public class UserEntity {
@Id
@Field(type = FieldType.Integer,store = true)
private Integer id;
@Field(type = FieldType.Text,store = true,analyzer = "ik_max_word")
private String username;
@Field(type = FieldType.Text,store = true,analyzer = "ik_max_word")
private String password;
@Field(type = FieldType.Text,store = true,analyzer = "ik_max_word")
private String name;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", name='" + name + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.itheima.component;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.org.apache.regexp.internal.RE;
import org.elasticsearch.client.Client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;

@Configuration
public class BeansCreate {

/**
* 将ObjectMapper作为一个bean交由spring管理
* @return
*/
@Bean
public ObjectMapper createObjectMapper(){
return new ObjectMapper();
}

/*@Bean
public ElasticsearchTemplate elasticsearchTemplate(Client client) {
return new ElasticsearchTemplate(client);
}*/

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.itheima.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.pojo.User;
import com.itheima.service.UserMapperService;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@Controller
public class UserController {

@Autowired
private UserService userService;

@Autowired
private UserMapperService userMapperService;

@Autowired
private StringRedisTemplate redisTemplate;

@Autowired
private ObjectMapper objectMapper;

/**
* spring整合redis
* 配置文件中配置redis的端口和ip---也可以配置redis集群
* @param id
* @return
*/
@RequestMapping("/user/{id}")
@ResponseBody
public User findById(@PathVariable int id) throws IOException {
//先从redis中取数据
String json = redisTemplate.opsForValue().get("user" + id);
//如果redis中部位不为null或者一个空串,则从redis中查数据
if (json!=null&&"".equals(json)){
//将json数据转换为字符串-->使用objectMapper转换(jackson下的)
User user = objectMapper.readValue(json, User.class);
return user;
}
//如果redis中没有数据则查询数据库
User user = userService.findById(id);
//并将数据缓存至redis中
redisTemplate.opsForValue().set("user"+id,objectMapper.writeValueAsString(user));
return user;
}

@RequestMapping("/user/page/list")
public String findAll(Model model){
List<User> userList = userService.findAll();
//将数据装入视图
model.addAttribute("userList",userList);
//返回对应视图解析器
return "user";
}

/**
* springboot整合mybatis
* 编写mybatis映射文件后,位置可以放在两个位置
* 1:当前mapper接口同级目录下 2:resources的相同目录下(///形式添加目录)
* 第二种无需解释,需要在springboot配置文件中配置mybatis的包扫描和别名映射(不必须)
* 第一种 默认在java目录下只编译源文件,想要编译xml文件需要在pom文件中添加build配置
* 还需要在springboot启动类中添加mapperscan注解扫描mybatis接口
* @param id
* @return
*/
@RequestMapping("/user/m/{id}")
@ResponseBody
public User findUserById(@PathVariable int id){
User user = userMapperService.findById(id);
return user;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.itheima.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class testController {

@RequestMapping("/test")
public String testBoot(){
return "hello boot";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.itheima.controller;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
*
* 取yml文件的值有两种方法:
* 1.@Value("${person.age}") 2:@ConfigurationProperties注解
*
* ConfigurationProperties注解会将springboot的.yml配置文件中的信息
* 自动映射为实体类的属性
* 使用:prefix="配置文件中需要映射的key的前缀"
*
* 注意:需要添加get/set方法
*/
@RestController
@ConfigurationProperties(prefix="person")
public class ymlTestController {

//@Value("${person.age}")
private Integer age;
//@Value("${person.address}")
private String address;
//@Value("${person.job}")
private String job;

@RequestMapping("/person")
public Map test(){
HashMap map = new HashMap();

map.put("age",age);
map.put("address",address);
map.put("job",job);

return map;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getJob() {
return job;
}

public void setJob(String job) {
this.job = job;
}
}
7 changes: 7 additions & 0 deletions springbootdemo/src/main/java/com/itheima/dao/UserDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.itheima.dao;

import com.itheima.pojo.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserDao extends JpaRepository<User,Integer>{
}
14 changes: 14 additions & 0 deletions springbootdemo/src/main/java/com/itheima/mapper/UserMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.itheima.mapper;

import com.itheima.pojo.User;
import org.mybatis.spring.annotation.MapperScan;

import java.util.List;


public interface UserMapper {

User findById(int id);

List<User> findAll();
}
13 changes: 13 additions & 0 deletions springbootdemo/src/main/java/com/itheima/mapper/UserMapper.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?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.itheima.mapper.UserMapper">
<select id="findById" resultType="com.itheima.pojo.User">
select * from user where id=#{id}
</select>

<select id="findAll" resultType="com.itheima.pojo.User">
select * from user
</select>
</mapper>
Loading

0 comments on commit 1860372

Please sign in to comment.