版权声明:本文为博主原创文章,未经博主允许不得转载 https://blog.csdn.net/Mythology_px/article/details/83185666
自定义转换器
-
package com.converter;
-
import java.text.ParseException;
-
import java.text.SimpleDateFormat;
-
import java.util.Date;
-
-
import org.springframework.core.convert.converter.Converter;
-
import org.springframework.stereotype.Component;
-
-
/*
-
* 设置类型转换器,将String类型日期进行转换成Date
-
*/
-
// 注解声明该类为一个组件类
-
@Component
-
public
class DateConverter implements Converter<String, Date> {
-
-
// 重写方法,进行数据类型转换
-
@Override
-
public Date convert(String source) {
-
SimpleDateFormat format =
new SimpleDateFormat(
"yyyy-MM-dd");
-
try {
-
Date date = format.parse(source);
-
return date;
-
}
catch (ParseException e) {
-
System.err.println(
"格式化日期失败");
-
}
-
return
null;
-
}
-
-
}
-
package com.converter;
-
-
import java.util.HashSet;
-
import java.util.Set;
-
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.context.annotation.Bean;
-
import org.springframework.context.annotation.Configuration;
-
import org.springframework.context.support.ConversionServiceFactoryBean;
-
import org.springframework.core.convert.ConversionService;
-
import org.springframework.core.convert.converter.Converter;
-
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
-
-
/*
-
* 将自定义的类型转换器进行注册配置
-
*/
-
// 注解声明该类为一个配置类
-
@Configuration
-
public
class ConverterConfig extends WebMvcConfigurerAdapter{
-
/*
-
* 配置日期类型转换器:
-
* 方法类型必须为ConversionService,参数为自定义的转换器;
-
* 注解声明按照类型自动注入bean
-
*/
-
@Bean
-
@Autowired
-
public ConversionService getConversionService(DateConverter dateConverter){
-
// 创建转换器工厂
-
ConversionServiceFactoryBean factory =
new ConversionServiceFactoryBean();
-
// 创建转换器集合,将转换器放入集合
-
Set<Converter> converters =
new HashSet<Converter>();
-
converters.add(dateConverter);
-
// 将转换器集合设置到工厂
-
factory.setConverters(converters);
-
// 返回转换器服务
-
return factory.getObject();
-
}
-
}
拦截器
-
package com.inteceptor;
-
-
import javax.servlet.http.HttpServletRequest;
-
import javax.servlet.http.HttpServletResponse;
-
-
import org.springframework.stereotype.Component;
-
import org.springframework.web.servlet.HandlerInterceptor;
-
import org.springframework.web.servlet.ModelAndView;
-
/*
-
* 设置拦截器,重写方法,对请求进行处理
-
*/
-
// 注解声明该类为一个组件类
-
@Component
-
public
class webInteceptor implements HandlerInterceptor{
-
-
@Override
-
public void afterCompletion(HttpServletRequest arg0,
-
HttpServletResponse arg1, Object arg2, Exception arg3)
-
throws Exception {
-
System.out.println(
"请求后进行处理.........");
-
}
-
-
@Override
-
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
-
Object arg2, ModelAndView arg3)
throws Exception {
-
System.out.println(
"请求时进行处理.........");
-
}
-
-
@Override
-
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
-
Object arg2)
throws Exception {
-
System.out.println(
"请求前进行处理.........");
-
/*
-
* 返回值:
-
* true:将拦截到的请求进行处理后,释放该请求,其他操作可以继续处理
-
* false:将拦截到的请求进行处理后,不释放该请求,请求中断
-
*/
-
return
true;
-
}
-
-
}
-
package com.inteceptor;
-
-
import org.springframework.context.annotation.Configuration;
-
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
-
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
-
/*
-
* 将拦截器处理类进行注册配置;@Configuration可能会导致页面跳转500,
-
* 可使用@EnableAutoConfiguration或者@EnableWebMvc注解解决
-
*/
-
// 注解声明该类为一个配置类
-
@EnableWebMvc
-
public
class InteceptorConfig extends WebMvcConfigurationSupport{
-
//重写addInterceptors方法,将拦截器类进行注册
-
@Override
-
public void addInterceptors(InterceptorRegistry registry) {
-
// 将拦截器类进行添加注册
-
registry.addInterceptor(
new webInteceptor());
-
}
-
}
文件上传
注意:form表单method="post" enctype="multipart/form-data"
-
package com.controller;
-
-
import java.io.File;
-
import java.io.IOException;
-
-
import javax.servlet.http.HttpServletRequest;
-
-
import org.springframework.stereotype.Controller;
-
import org.springframework.web.bind.annotation.RequestMapping;
-
import org.springframework.web.bind.annotation.RequestParam;
-
import org.springframework.web.multipart.MultipartFile;
-
/*
-
* 文件上传
-
*/
-
@Controller
-
public
class LoginController {
-
@RequestMapping(
"/login")
-
public String subLogin(@RequestParam("file") MultipartFile file,HttpServletRequest request){
-
// 获取文件名字
-
String fileName = file.getOriginalFilename();
-
System.out.println(fileName);
-
// 获取服务器绝对路径
-
String path = request.getSession().getServletContext().getRealPath(
"/img/");
-
System.out.println(path);
-
// 创建文件
-
File newFile =
new File(path+fileName);
-
// 写出文件
-
try {
-
file.transferTo(newFile);
-
}
catch (IllegalStateException e) {
-
System.err.println(
"文件上传失败");
-
}
catch (IOException e) {
-
System.err.println(
"文件传输失败");
-
}
-
return
"index";
-
}
-
}
Springboot整合mybatis
1. 配置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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-
-
<modelVersion>4.0.0
</modelVersion>
-
<groupId>com.mo
</groupId>
-
<artifactId>Micro
</artifactId>
-
<version>0.0.1-SNAPSHOT
</version>
-
<name>springboot-mybatis
</name>
-
-
<!-- 引入springboot父项目 -->
-
<parent>
-
<groupId>org.springframework.boot
</groupId>
-
<artifactId>spring-boot-starter-parent
</artifactId>
-
<version>1.5.17.RELEASE
</version>
-
</parent>
-
-
<!-- 添加starter依赖- -->
-
<dependencies>
-
<!-- 引入web-starter依赖 -->
-
<dependency>
-
<groupId>org.springframework.boot
</groupId>
-
<artifactId>spring-boot-starter-web
</artifactId>
-
</dependency>
-
-
<!-- 引入jsp的依赖 -->
-
<dependency>
-
<groupId>javax.servlet
</groupId>
-
<artifactId>jstl
</artifactId>
-
</dependency>
-
-
<!-- 引入tomcat的部分依赖:scope作用域,provided在进行部署项目时,不添加此jar -->
-
<dependency>
-
<groupId>org.apache.tomcat.embed
</groupId>
-
<artifactId>tomcat-embed-jasper
</artifactId>
-
<scope>provided
</scope>
-
</dependency>
-
-
<!-- 引入mybatis的依赖 -->
-
<dependency>
-
<groupId>org.mybatis.spring.boot
</groupId>
-
<artifactId>mybatis-spring-boot-starter
</artifactId>
-
<version>1.1.1
</version>
-
</dependency>
-
-
<!-- mysql数据库驱动 -->
-
<dependency>
-
<groupId>mysql
</groupId>
-
<artifactId>mysql-connector-java
</artifactId>
-
</dependency>
-
-
<!-- druid数据库连接池 -->
-
<dependency>
-
<groupId>com.alibaba
</groupId>
-
<artifactId>druid
</artifactId>
-
<version>1.0.9
</version>
-
</dependency>
-
</dependencies>
-
-
<!-- 配置可以将项目打包jar -->
-
<build>
-
<plugins>
-
<plugin>
-
<groupId>org.springframework.boot
</groupId>
-
<artifactId>spring-boot-maven-plugin
</artifactId>
-
</plugin>
-
</plugins>
-
</build>
-
</project>
2. 在resources下创建和配置全局配置文件:application.properties
spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=UTF-8 spring.datasource.username=root spring.datasource.password= spring.datasource.type=com.alibaba.druid.pool.DruidDataSource mybatis.type-aliases-package=com.entity
注:配置文件配置页面视图解析,数据库连接信息,数据源类型,映射文件别名
3. 创建mapper层接口:接口类和xml配置文件名字须一致
-
package com.mapper;
-
-
import com.entity.Student;
-
/*
-
* 数据库映射接口:由配置映射文件执行sql语句
-
*/
-
public
interface StudentMapper {
-
public int insert(Student stu);
-
}
-
<?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.mapper.StudentMapper">
-
<!-- mybatis接口映射文件 -->
-
<insert id="insert" parameterType="Student">
-
insert into student(sname,score,addr) values(#{sname},#{score},#{addr})
-
</insert>
-
</mapper>
4. 创建service层业务处理
-
package com.service;
-
-
import com.entity.Student;
-
/*
-
* service层接口业务
-
*/
-
public
interface StudentService {
-
public boolean insert(Student stu);
-
}
-
package com.service;
-
-
import javax.annotation.Resource;
-
-
import org.springframework.stereotype.Service;
-
import org.springframework.transaction.annotation.Transactional;
-
-
import com.entity.Student;
-
import com.mapper.StudentMapper;
-
/*
-
* 业务层实现
-
*/
-
@Service
-
// 注解添加事务:在service层添加业务,可以防止操作失败导致数据库数据错误提交
-
@Transactional
-
public
class StudentServiceImp implements StudentService {
-
@Resource
-
private StudentMapper studentMapper;
-
-
@Override
-
public boolean insert(Student stu) {
-
int rows = studentMapper.insert(stu);
-
if (rows >
0) {
-
return
true;
-
}
else {
-
return
false;
-
}
-
}
-
-
}
5. 创建controller层进行交互
-
package com.controller;
-
-
import javax.annotation.Resource;
-
-
import org.springframework.stereotype.Controller;
-
import org.springframework.web.bind.annotation.RequestMapping;
-
import org.springframework.web.bind.annotation.ResponseBody;
-
-
import com.entity.Student;
-
import com.service.StudentService;
-
/*
-
* 控制层:与前台进行交互和页面跳转
-
*/
-
@Controller
-
public
class StudentController {
-
@Resource
-
private StudentService studentService;
-
-
@ResponseBody
-
@RequestMapping(
"/insert")
-
public String insert(){
-
Student stu =
new Student(
null,
"Micro",
78,
"咸阳");
-
boolean flag = studentService.insert(stu);
-
if(flag){
-
return
"插入成功";
-
}
else{
-
return
"插入失败";
-
}
-
}
-
}
前行的路上总是充满荆棘和诱惑的, 不忘初心,坚持,就会看到希望
----墨渐生微