前言:
小白笔记。
正文:
一、接入Log4j。
可参考这篇文章。唯一不同的就是依赖jar包通过Maven的工具直接查找。
二、写一个根据中文名搜索的请求
1、写一个Service接口
package service;
import java.util.List;
import model.Book;
public interface IBookService {
Book getBookById(String bookId);
List<Book> getBookByName(String bookName);
}
2、上一篇文章,之前通过Mybatis自动生成工具生成的代码查询Id,本文基于上文添加。
2.1、dao层添加:
package dao;
import java.util.List;
import model.Book;
public interface BookMapper {
int deleteByPrimaryKey(String id);
int insert(Book record);
int insertSelective(Book record);
Book selectByPrimaryKey(String id);
//添加搜姓名
List<Book> selectByName(String bookName);
int updateByPrimaryKeySelective(Book record);
int updateByPrimaryKeyWithBLOBs(Book record);
int updateByPrimaryKey(Book record);
}
2.2、Mapping层(其中添加注释的两处地方为本次添加)
<?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="dao.BookMapper">
<resultMap id="BaseResultMap" type="model.Book">
<id column="id" jdbcType="VARCHAR" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="chapter_name" jdbcType="VARCHAR" property="chapterName" />
<result column="owner" jdbcType="VARCHAR" property="owner" />
</resultMap>
<resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="model.Book">
<result column="chapter_content" jdbcType="LONGVARCHAR" property="chapterContent" />
</resultMap>
<!--BookList: Book对应Model模块下Book类 -->
<resultMap id="BookList" type="model.Book">
<id column="id" jdbcType="VARCHAR" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="chapter_name" jdbcType="VARCHAR" property="chapterName" />
<result column="owner" jdbcType="VARCHAR" property="owner" />
<result column="chapter_content" jdbcType="LONGVARCHAR" property="chapterContent" />
</resultMap>
<sql id="Base_Column_List">
id, name, chapter_name, owner
</sql>
<sql id="Blob_Column_List">
chapter_content
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="ResultMapWithBLOBs">
select
<include refid="Base_Column_List" />
,
<include refid="Blob_Column_List" />
from biquge
where id = #{id,jdbcType=VARCHAR}
</select>
<!-- 添加搜索姓名对应,其中BookList在上面 -->
<select id="selectByName" parameterType="java.lang.String" resultMap="BookList">
select id, name, chapter_name, owner , chapter_content from biquge where name = #{name,jdbcType=VARCHAR};
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.String">
delete from biquge
where id = #{id,jdbcType=VARCHAR}
</delete>
<insert id="insert" parameterType="model.Book">
insert into biquge (id, name, chapter_name,
owner, chapter_content)
values (#{id,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{chapterName,jdbcType=VARCHAR},
#{owner,jdbcType=VARCHAR}, #{chapterContent,jdbcType=LONGVARCHAR})
</insert>
<insert id="insertSelective" parameterType="model.Book">
insert into biquge
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="name != null">
name,
</if>
<if test="chapterName != null">
chapter_name,
</if>
<if test="owner != null">
owner,
</if>
<if test="chapterContent != null">
chapter_content,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=VARCHAR},
</if>
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
<if test="chapterName != null">
#{chapterName,jdbcType=VARCHAR},
</if>
<if test="owner != null">
#{owner,jdbcType=VARCHAR},
</if>
<if test="chapterContent != null">
#{chapterContent,jdbcType=LONGVARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="model.Book">
update biquge
<set>
<if test="name != null">
name = #{name,jdbcType=VARCHAR},
</if>
<if test="chapterName != null">
chapter_name = #{chapterName,jdbcType=VARCHAR},
</if>
<if test="owner != null">
owner = #{owner,jdbcType=VARCHAR},
</if>
<if test="chapterContent != null">
chapter_content = #{chapterContent,jdbcType=LONGVARCHAR},
</if>
</set>
where id = #{id,jdbcType=VARCHAR}
</update>
<update id="updateByPrimaryKeyWithBLOBs" parameterType="model.Book">
update biquge
set name = #{name,jdbcType=VARCHAR},
chapter_name = #{chapterName,jdbcType=VARCHAR},
owner = #{owner,jdbcType=VARCHAR},
chapter_content = #{chapterContent,jdbcType=LONGVARCHAR}
where id = #{id,jdbcType=VARCHAR}
</update>
<update id="updateByPrimaryKey" parameterType="model.Book">
update biquge
set name = #{name,jdbcType=VARCHAR},
chapter_name = #{chapterName,jdbcType=VARCHAR},
owner = #{owner,jdbcType=VARCHAR}
where id = #{id,jdbcType=VARCHAR}
</update>
</mapper>
3、写接口的实现,其中getBookByName方法中调用上面添加的查询
package service;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import dao.BookMapper;
import model.Book;
@Service("userService")
public class BookServiceImpl implements IBookService{
@Resource
private BookMapper bookDao;
public Book getBookById(String bookId) {
// TODO Auto-generated method stub
return this.bookDao.selectByPrimaryKey(bookId);
}
public List<Book> getBookByName(String bookName) {
return this.bookDao.selectByName(bookName);
}
}
4、写一个Controller
@Controller
public class HelloController {
private static Logger log = LoggerFactory.getLogger(HelloController.class);
@Resource
private IBookService iBookService;
@RequestMapping(value="d.do",produces="text/html;charset=UTF-8")
public @ResponseBody String viewBook() {
List<Book> bl = iBookService.getBookByName("大明春色");
System.out.println(bl.toString());
//添加error日志
log.error("----log-----");
return "你好";
}
}
问题一、根据日志,发现查询出来结果为0条。原因是搜索的中文utf-8。所以在数据库的连接配置文件上添加如下:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ebook?useUnicode=true&characterEncoding=UTF-8
username=root
password=haibo1118
initialSize=5
然后就能查出很多条结果了。
5、改造成一个能修改参数的Get请求
觉得我这个固定的搜索名字不像一个请求?那么我就改改Controller即可。get请求如下:http://localhost:8080/mvnStudy01/e.do?name=大明春色
package controller;
import java.util.List;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import model.Book;
import service.IBookService;
@Controller
public class HelloController {
private static Logger log = LoggerFactory.getLogger(HelloController.class);
@Resource
private IBookService iBookService;
@RequestMapping(value="d.do",produces="text/html;charset=UTF-8")
public @ResponseBody String viewBook() {
List<Book> bl = iBookService.getBookByName("大明春色");
System.out.println(bl.toString());
//添加error日志
log.error("----log-----");
return "你好";
}
@RequestMapping(value="e.do",produces="text/html;charset=UTF-8")
public @ResponseBody String viewBookByName(@RequestParam(name="name",defaultValue="大明春色")String name) {
List<Book> bl = iBookService.getBookByName(name);
System.out.println(bl.toString());
//添加error日志
log.error("----log-----");
return "你好";
}
}
6、Post请求
package controller;
import java.util.List;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import model.Book;
import service.IBookService;
@Controller
public class HelloController {
private static Logger log = LoggerFactory.getLogger(HelloController.class);
@Resource
private IBookService iBookService;
@RequestMapping(value="d.do",produces="text/html;charset=UTF-8")
public @ResponseBody String viewBook() {
List<Book> bl = iBookService.getBookByName("大明春色");
System.out.println(bl.toString());
//添加error日志
log.error("----log-----");
return "你好";
}
@RequestMapping(value="e.do",produces="text/html;charset=UTF-8")
public @ResponseBody String viewBookByName(@RequestParam(name="name",defaultValue="大明春色")String name) {
List<Book> bl = iBookService.getBookByName(name);
System.out.println(bl.toString());
//添加error日志
log.error("----log-----");
return "你好";
}
// Post请求
@RequestMapping(value="f.do",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
public @ResponseBody String postBookByName(String name) {
System.out.println(name.toString());
List<Book> bl = iBookService.getBookByName(name);
//添加error日志
log.error("----log-----"+bl.toString());
return "你好";
}
}
问题二、Post请求时name原本是中文,变成了接收到的是乱码
大明春色
解决方案:在web.xml中添加如下代码:
<!-- 配置springMVC编码过滤器 -->
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
如果添加之后没效果,重启服务即可看到Post请求的中文乱码问题被解决。7、Rest风格的get请求改造。
Controller代码如下:即通过PathVariable绑定入参和URL
// get请求,Rest风格
@RequestMapping(value="/view/{name}",method=RequestMethod.GET,produces="text/html;charset=UTF-8")
public @ResponseBody String restBookByName(@PathVariable("name")String name) {
List<Book> bl = iBookService.getBookByName(name);
System.out.println(bl.toString());
//添加error日志
log.error("----log-----");
return "你好";
}
请求连接如下:http://localhost:8080/mvnStudy01/view/大明春色
这样就可以访问get请求了,当然比之前更加优雅。
PS:此处我对之前web.xml修改了过滤,改成了对所有请求过滤/,web.xml其中的修改部分如下
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
8、HttpServlet+SpringMVC风格结合:
我就是喜欢这种古朴的风格,那怎么写?
//古老风格HttpServlet与SpringMVC的结合
@RequestMapping(value="/view2",method=RequestMethod.GET,produces="text/html;charset=UTF-8")
public @ResponseBody String httpServletBookByName(HttpServletRequest request) {
List<Book> bl = iBookService.getBookByName(request.getParameter("name"));
System.out.println(bl.toString());
//添加error日志
log.error("----log-----");
return "你好:古老风格HttpServlet与SpringMVC的结合";
}
请求如下:
http://localhost:8080/mvnStudy01/view2?name=大明春色
完美!到此为止。