【汇智学堂】springcloud+springboot+ssm(jsp显示)
作者:互联网
when you run,please do as this:
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.huizhi</groupId>
<artifactId>orderservice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>orderservice</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</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>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 连接数据库的代码-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- 添加jsp的maven依赖-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties:
#数据库指向,school是我的数据库名,改成自己的
spring.datasource.url=jdbc:mysql://localhost:3306/mymis?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
#数据库用户名
spring.datasource.username=root
#数据库密码
spring.datasource.password=root
#出现mysql jdbc标红报错问题------>去pom.xml中删除mysql-connector-java下面的runtime那一行
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.database = mysql
#Mybatis扫描
mybatis.mapper-locations=classpath*:mapper/*.xml
#起别名。可省略写mybatis的xml中的resultType的全路径
mybatis.type-aliases-package=com.huizhi.demo.po
spring.rsocket.server.port=8080
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
UserController:
package com.huizhi.orderservice.controller;
import com.huizhi.orderservice.po.User;
import com.huizhi.orderservice.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserController {
// @Bean
// public InternalResourceViewResolver viewResolver() {
// InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
// viewResolver.setPrefix("/WEB-INF/jsp/");
// viewResolver.setSuffix(".jsp");
// return viewResolver;
// }
//注入用户Service
@Autowired
private UserService userService;
//查询所有用户
// public List<User> getAllUsers(){
// List<User> list=this.userService.getAllUsers();
// return list;
// }
@RequestMapping("/userList")
public String getAllUsers(Model model){
List<User> list=this.userService.getAllUsers();
model.addAttribute("userList",list);
//return "user";//静态页面访问
return "a";//jsp页面
}
//删除用户
@RequestMapping("/delete/{id}")
public String delete(@PathVariable Integer id,Model model){
this.userService.deleteUser(id);
List<User> list=this.userService.getAllUsers();
model.addAttribute("userList",list);
//return "user";//静态页面访问
return "a";//jsp页面
}
}
UserMapper:
package com.huizhi.orderservice.dao;
import com.huizhi.orderservice.po.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Mapper
@ResponseBody
public interface UserMapper {
//查询所有用户
@Select("select*from tb_user")
List<User> getAllUsers();
//删除所有用户
@Delete("delete from tb_user where id=#{id}")
void delte(Integer id);
}
User:
package com.huizhi.orderservice.po;
import java.io.Serializable;
public class User implements Serializable {
private Integer id;
private String userName;
private String address;
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 getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
UserService:
package com.huizhi.orderservice.service;
import com.huizhi.orderservice.po.User;
import java.util.List;
public interface UserService {
//查询所有
List<User> getAllUsers();
//删除数据
void deleteUser(Integer id);
}
UserServiceImpl:
package com.huizhi.orderservice.service.impl;
import com.huizhi.orderservice.dao.UserMapper;
import com.huizhi.orderservice.po.User;
import com.huizhi.orderservice.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional
public class UserServiceImpl implements UserService {
//注入用户Mapper
@Autowired(required=false)
private UserMapper userMapper;
//查询所有用户
@Cacheable(value="UserCache",key="'user.getAllUsers'")
public List<User> getAllUsers(){
return this.userMapper.getAllUsers();
}
//删除用户
@CacheEvict(value = "UserCache",key = "'user.getAllUsers'")
public void deleteUser(Integer id){
this.userMapper.delte(id);
System.out.println("删除了id为"+id+"的用户");
}
}
雷玉广
发布了268 篇原创文章 · 获赞 47 · 访问量 3万+
私信
关注
标签:springboot,spring,springcloud,汇智,springframework,id,org,import,public 来源: https://blog.csdn.net/weixin_39593940/article/details/103938666