标签:under ada eth space 账户 use spring int 二级缓存
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--日志我们使用logback-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
<scope>provided</scope>
</dependency>
<!-- ***************数据库相关配置****************** -->
<!-- mysql驱动依赖包,连接mysql必备-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<!-- 数据源依赖包,能大幅提升性和便利性。这里我们用阿里的德鲁伊数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<!-- Mybatis必备依赖包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- ***************web相关配置****************** -->
<!--配置JavaEE依赖包,包含了Servlet、Validation等功能-->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
<!-- JSTL依赖包,如果用将jstl标签用在jsp中就要此依赖包。非必需的 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- jackson依赖包,用来将java对象转换JSON格式,SpringMVC要用的 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.10.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.12.0</version>
</dependency>
<!-- ***************Spring相关配置****************** -->
<!--配置Spring JDBC容器所需的jar包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<!--配置Spring IOC容器所需的jar包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<!--Spring mvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<!-- AspectJ所需的jar包-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<!--Spring测试依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.6.RELEASE</version>
<scope>test</scope>
</dependency>
<!--配置Spring整合mybatis的jar包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!--jackson引入依赖-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.4</version>
</dependency>
</dependencies>
CREATE TABLE `user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT ‘主键id‘,
`name` varchar(255) COLLATE utf8mb4_bin NOT NULL COMMENT ‘账户名‘,
`password` varchar(255) COLLATE utf8mb4_bin NOT NULL COMMENT ‘账户密码‘,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
INSERT INTO `user` VALUES (1, ‘admin‘, ‘123456‘);
INSERT INTO `user` VALUES (2, ‘rudecrab‘, ‘654321‘);
然后,我们需要再entity包下创建一个与数据库表对应的类User。
@Data
@NoArgsConstructor
public class User implements Serializable {
private Long id;
private String name;
private String password;
}
数据库类创建好后,我们需要来配置数据库连接相关的属性,来与我们的mybatis进行整合。(账号、密码根据个人情况进行更改,mysql 8.0以后的驱动尽量用com.mysql.cj.jdbc.Driver)
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?userSSl=false&serverTimezone=GMT&characterEncoding=utf8
jdbc.username=root
jdbc.password=root
下面我们对Spring和Mybatis进行整合。我们需要在resources文件夹下新建mybatis-config.xml文件,来对Mybatis进行全局配置。
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--全局配置-->
<settings>
<setting name="logImpl" value="SLF4J"/>
<!--启用主键生成策略-->
<setting name="useGeneratedKeys" value="true"/>
<!--配置启用下划线转驼峰的映射-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!--启用二级缓存-->
<setting name="cacheEnabled" value="true"/>
</settings>
</configuration>
接下来再来新建spring-mybatis.xml文件,这个文件用作整合的。有不懂得地方,可以看注释。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--属性文件的读取,这里读取刚才我们的数据库连接相关配置-->
<context:property-placeholder location="classpath:database.properties" file-encoding="UTF-8"/>
<!--配置自动扫描,如果不配置这个那么就无法使用@Autowired加载bean-->
<context:component-scan base-package="com.lx.ssm" use-default-filters="true">
<!--这里要排除掉Controller的注解,Controller专门交给MVC去扫描,这样会就不会冲突-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--配置数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!--配置JDBC基础属性,即数据库连接相关配置-->
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!--配置连接池的设置,这个是要根据真实项目情况来配置的,随着项目的发展会不断修改-->
<property name="initialSize" value="10"/>
<property name="maxActive" value="100"/>
</bean>
<!--
重点来了,这里配置是MyBatis的SqlSessionFactory,就是这一块配置将Spring和Mybatis整合到了一起
如果不配置这里,你的mapper接口只能通过SqlSession来获取,十分麻烦。这里配置好后就可以通过Spring IoC来获取mapper接口了
-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<!--指定数据源-->
<property name="dataSource" ref="dataSource"/>
<!--加载mybatis全局设置,classpath即我们的资源路径resources-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--配置Mybatis的映射xml文件路径-->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!--指定Mybatis的mapper接口扫描包-->
<!--注意!!!如果用的是tk.mybatis自动生成的mapper接口,一定要将org.mybatis.改成tk.mybatis-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--指定刚才我们配置好的sqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!--指定mapper接口扫描包-->
<property name="basePackage" value="com.lx.ssm.mapper"/>
</bean>
<!--配置事务管理器,如果不配置这个,不启动事务扫描的话,那么发生了异常也不会触发回滚-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--还得指定数据源-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--启动事务的扫描-->
<tx:annotation-driven/>
</beans>
以防万一,我们先将Spring与Mybatis的整合,进行测试。
UserMapper接口专门用来声明各种数据库操作的方法,@Repository注解将其定义为Spring所管理的bean:
@Repository
public interface UserMapper {
List<User> selectAll();
}
UserMapper.xml映射文件用来写方法对应要执行的SQL语句:
<?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.lx.ssm.mapper.UserMapper">
<cache/>
<!--从数据库查出所有的User对象-->
<select id="selectAll" resultType="com.lx.ssm.entity.User">
select * from user
</select>
</mapper>
UserService接口用来声明关于User的业务方法:
public interface UserService {
// 返回User对象集合
List<User> getAll();
}
UserServiceImpl实体类用来实现关于User的业务逻辑,@Service注解和@Repository注解用处一样,将其定义为bean。@Transactional注解为声明式事务。
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> getAll() {
return userMapper.selectAll();
}
}
测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:spring-mybatis.xml"})
public class UserServiceTest {
@Autowired
private UserService userService;
@Test
public void getAll(){
System.out.println(userService.getAll());
}
}
测试成功结果:
Spring与Mybatis的整合测试成功,我们下面来配置SpringMVC。在resources文件夹下创建spring-mvc.xml文件进行配置。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--配置视图解析器,这样控制器里就直接返回文件名就好了-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀-->
<!-- <property name="prefix" value="/WEB-INF/views/"/>-->
<property name="prefix" value="/WEB-INF/view/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</list>
</property>
</bean>
<!--配置静态资源过滤,不然静态资源比如css是访问不到的-->
<mvc:default-servlet-handler/>
<!--配置扫描的包-->
<context:component-scan base-package="com.lx.ssm.controller" use-default-filters="false">
<!--只扫描controller,实际开发中最好用这种方式来写,这边MVC就只扫描controller,就不会IOC那边冲突,否则事务会被覆盖,IOC那边就要排除这个controller-->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--启用MVC的注解-->
<mvc:annotation-driven/>
</beans>
web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!--1.配置Spring IOC容器的创建,如果不配置这个,Mybatis就在web应用里无法使用-->
<context-param>
<param-name>contextConfigLocation</param-name>
<!--spring和mybatis整合配置文件路径-->
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--2.配置SpringMVC的前端控制器-->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!--SpringMVC整合配置文件路径-->
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--3.配置字符编码过滤器-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
最终测试
在WEB-INF文件夹下,创建view文件夹,在此目录下创建index.jsp文件:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>首页-original</title>
</head>
<body>
<%--循环提取userList中的元素--%>
<c:forEach var="user" items="${userList}">
<ul>
<li>${user}</li>
</ul>
</c:forEach>
</body>
</html>
接下来在controller包下新建一个控制器类类定义访问接口:
@Controller
@RequestMapping(value = "user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/getList")
public String getList(Model model){
/*将数据存到model对象里,使jsp可以访问数据*/
model.addAttribute("userList",userService.getAll());
/*返回jsp文件名*/
return "index";
}
@GetMapping("/getJson")
@ResponseBody
public List<User> getList(){
return userService.getAll();
}
}
测试成功:
标签:under ada eth space 账户 use spring int 二级缓存
原文地址:https://www.cnblogs.com/eason-xun/p/14545202.html