自定义转换器
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 {
// 重写方法,进行数据类型转换
@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 converters = new HashSet();
converters.add(dateConverter);
// 将转换器集合设置到工厂
factory.setConverters(converters);
// 返回转换器服务
return factory.getObject();
}
}
拦截器
package com.merit.inteceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import java.lang.reflect.Method;
/*
* 设置拦截器,重写方法,对请求进行处理
*/
// 注解声明该类为一个组件类
@Component
public class webInteceptor implements HandlerInterceptor {
/**
* @param request 请求
* @param response 响应
* @param handlerMethod 处理方法的封装
* @param exception 异常
* @throws Exception
*/
@Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handlerMethod, Exception exception)
throws Exception {
HandlerMethod handler = (HandlerMethod) handlerMethod;
// 获取处理请求的方法
Method method = handler.getMethod();
System.out.println(method);
System.out.println("请求后进行处理.........");
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response,
Object handlerMethod, ModelAndView exception) throws Exception {
System.out.println("请求时进行处理.........");
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handlerMethod) throws Exception {
System.out.println("请求前进行处理.........");
/*
* 返回值:
* true:将拦截到的请求进行处理后,释放该请求,其他操作可以继续处理
* false:将拦截到的请求进行处理后,不释放该请求,请求中断
*/
return true;
}
}
package com.merit.inteceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/*
* 将拦截器处理类进行注册配置;@Configuration可能会导致页面跳转500,
* 可使用@EnableAutoConfiguration或者@EnableWebMvc注解解决
*/
// 注解声明该类为一个配置类
@Configuration
public class InteceptorConfig extends WebMvcConfigurerAdapter {
//重写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依赖
4.0.0
com.mo
Micro
0.0.1-SNAPSHOT
springboot-mybatis
org.springframework.boot
spring-boot-starter-parent
1.5.17.RELEASE
org.springframework.boot
spring-boot-starter-web
javax.servlet
jstl
org.apache.tomcat.embed
tomcat-embed-jasper
provided
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.1.1
mysql
mysql-connector-java
com.alibaba
druid
1.0.9
org.springframework.boot
spring-boot-maven-plugin
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);
}
insert into student(sname,score,addr) values(#{sname},#{score},#{addr})
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 "插入失败";
}
}
}
前行的路上总是充满荆棘和诱惑的, 不忘初心,坚持,就会看到希望
----墨渐生微