之前看了一些文章,有的使用注解,有的使用一些功能比较强大的类,都不太好理解,所以我写了一篇最易读的,没有注解,没有强大功能的类,都是最基本的,特别容易理解,适合从零开始自学的学者。
1.首先篇写大环境,导入依赖,并处理静态资源导出问题
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
2.利用ORM思想,写出数据库中对应的实体类
package com.fei.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books {
private int bookId;
private String bookName;
private int bookCounts;
private String detail;
}
3.dao层
3.1dao层接口
package com.fei.dao;
import com.fei.pojo.Books;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface BookMapper {
//增加一本书
public int addBook(Books books);
//根据id删除一本书
public int delete(int bookId);
//修改一本书
public int update(Books books);
//查询所有书籍
public List<Books> selectAll();
//根据id名查询书籍信息
public Books selectById(int bookId);
}
3.2dao层实现类
package com.fei.dao;
import com.fei.pojo.Books;
import org.mybatis.spring.SqlSessionTemplate;
import java.util.List;
public class BookMapperImpl implements BookMapper {
private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
public int addBook(Books books) {
BookMapper mapper = sqlSession.getMapper(BookMapper.class);
return mapper.addBook(books);
}
public int delete(int bookId) {
BookMapper mapper = sqlSession.getMapper(BookMapper.class);
return mapper.delete(bookId);
}
public int update(Books books) {
BookMapper mapper = sqlSession.getMapper(BookMapper.class);
return mapper.update(books);
}
public List<Books> selectAll() {
BookMapper mapper = sqlSession.getMapper(BookMapper.class);
return mapper.selectAll();
}
public Books selectById(int bookId) {
BookMapper mapper = sqlSession.getMapper(BookMapper.class);
return mapper.selectById(bookId);
}
}
3.3dao层配置文件
<?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.fei.dao.BookMapper">
<!--1.增加的sql映射-->
<insert id="addBook" parameterType="Books">
insert into books (bookId,bookName, bookCounts, detail)
values (#{
bookId},#{
bookName},#{
bookCounts},#{
detail});
</insert>
<!--2.删除的sql映射-->
<delete id="delete">
delete from books where bookId=#{
bookId}
</delete>
<!--3.修改的sql映射-->
<update id="update" parameterType="Books">
update books set bookName=#{
bookName},bookCounts=#{
bookCounts},detail=#{
detail} where bookId=#{
bookId}
</update>
<!--4.查询的sql映射-->
<select id="selectAll" resultType="Books">
select * from ssmbuild.books
</select>
<!--5.根据id查询的sql映射-->
<select id="selectById" resultType="Books">
select * from books where bookId=#{
bookId}
</select>
</mapper>
4.service层
4.1service层接口
package com.fei.servie;
import com.fei.pojo.Books;
import java.util.List;
public interface BookService {
//增加一本书
public int addBook(Books books);
//根据id删除一本书
public int delete(int bookId);
//修改一本书
public int update(Books books);
//查询所有书籍
public List<Books> selectAll();
//根据id名查询书籍信息
public Books selectById(int bookId);
}
4.2service层实现类
package com.fei.servie;
import com.fei.dao.BookMapperImpl;
import com.fei.pojo.Books;
import java.util.List;
public class BookServiceImpl implements BookService {
private BookMapperImpl boolMapper;
public void setBoolMapper(BookMapperImpl boolMapper) {
this.boolMapper = boolMapper;
}
public int addBook(Books books) {
return boolMapper.addBook(books);
}
public int delete(int bookId) {
return boolMapper.delete(bookId);
}
public int update(Books books) {
return boolMapper.update(books);
}
public List<Books> selectAll() {
return boolMapper.selectAll();
}
public Books selectById(int bookId) {
return boolMapper.selectById(bookId);
}
}
5.controller层(注意:这种实现controller接口的方式,不太好,因为一个控制器只能实现一个方法,在下一篇文章中会有优化)
package com.fei.controller;
import com.fei.pojo.Books;
import com.fei.servie.BookService;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
public class Controller1 implements Controller {
private BookService bookService;
public void setBookService(BookService bookService) {
this.bookService = bookService;
}
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
ModelAndView mv = new ModelAndView();
List<Books> list = bookService.selectAll();
mv.addObject("msg",list);
mv.setViewName("test");
return mv;
}
}
6.配置文件:
6.1web.xml的配置
在web.xml中主要配置前端总控制器DispqtcherServlet和乱码过滤器
<?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_4_0.xsd"
version="4.0">
<!--DispathServlet-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-context.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>
<!--乱码过滤-->
<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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*
15
6.2mybatis的配置
在mybatis-config.xml文件中配置别名问题,数据库的连接和mapper的对应配置都交给spring-dao层去做
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias type="com.fei.pojo.Books" alias="Books"/>
</typeAliases>
</configuration>
6.3配置数据源db.properties
jdbc.driver=com.mysql.jdbc.Driver
#如果使用的是MySql8.0需要增加一个时区设置
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?serverTimezone=GMT&useSSL=true&useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=123456
6.4spring-dao层的配置
<?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"
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">
//将数据源导入
<context:property-placeholder location="classpath:db.properties"/>
<!--1.将原先在mybatis中的数据库转移到mybatis-->
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--2.配置Sqlsessionfactory-->
<bean id="sqlsessionfactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/fei/dao/BookMapper.xml"/>
</bean>
<!-- 3.配置sqlsession -->
<bean id="sqlsession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlsessionfactory"/>
</bean>
<!--配置BookMapperImpl-->
<bean id="BookMapperImpl" class="com.fei.dao.BookMapperImpl">
<property name="sqlSession" ref="sqlsession"/>
</bean>
</beans>
6.5spring-service层的配置:将service的实现类配置即可
<?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"
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">
<bean id="BookServiceImpl" class="com.fei.servie.BookServiceImpl">
<property name="boolMapper" ref="BookMapperImpl"/>
</bean>
</beans>
6.6spring-mvc层的配置(对应controller层):配置处理器映射器、处理器适配器、视图解析器以及将控制器注入
<?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.handler.BeanNameUrlHandlerMapping"/>
<!--处理器适配器-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="/hello" class="com.fei.controller.Controller1">
<property name="bookService" ref="BookServiceImpl"/>
</bean>
</beans>
6.7配置核心文件applicationContext.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"
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">
<import resource="Spring-dao.xml"/>
<import resource="Spring-service.xml"/>
<import resource="Spring-mvc.xml"/>
</beans>
7.由于此处,只是做简单的郑和所以前端只是输出,没有去做渲染工作
7.1index.xml文件没有修改
7.2在WEB-INF下建立目录jsp,然后创建text.jsp文件,直接输出
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2020/4/4 0004
Time: 18:20
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${
msg}
</body>
</html>
到此,就可以去开启tomcat运行啦。。。下一层开始优化!!