转载

搭建工程

  • 创建 Maven 工程
  • 添加 web.xmltomcat 运行环境
  • 创建包的结构
    • 实体类的包: cn.tedu.store.bean
    • 控制器层: cn.tedu.store.controller
    • 业务层 : cn.tedu.store.service
    • 持久层 : cn.tedu.store.mapper

搭建工程

  • 添加依赖 pom.xml
    • spring-webmvc
    • spring-jdbc
    • mybatis
    • mybatis-spring
    • commons-dbcp
    • jackson
    • junit
    • jstl
<dependencies>
		<!-- SpringMVC -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>3.2.8.RELEASE</version>
		</dependency>

		<!-- Spring-JDBC,要和spring-webmvc的版本一致 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>3.2.8.RELEASE</version>
		</dependency>

		<!-- MyBatis -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.4.6</version>
		</dependency>

		<!-- MyBatis-Spring 整合jar包 -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.3.2</version>
		</dependency>

		<!-- MySQL驱动jar包 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.28</version>
		</dependency>

		<!-- DBCP连接池-->
		<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>1.4</version>
		</dependency>

		<!-- 添加jackson,自动转换为JSON数据 -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.9.4</version>
		</dependency>

		<!-- 添加jstl标签库 用于jsp页面标签 -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>

		<!-- Junit -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
		</dependency>

	</dependencies>
  • 添加配置文件( src/main/resource 文件下)

    • 创建一个新的文件夹 mappers ,存放 mybaits 的映射文件: XXXMapper.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
     "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
     
     <!-- namespace指定的UserMapper的全类名 -->
     <mappernamespace="cn.tedu.store.mapper.UserMapper">
     
     
     
     
     </mapper>
    

    • 创建一个 db.properties 文件,其中存放的是 dbcp 连接池的配置属性
    url=jdbc:mysql://localhost:3306/tedu_store?useUnicode=true&characterEncoding=utf8
    driver=com.mysql.jdbc.Driver
    user=root
    password=root
    initSize=2
    maxSize=10
    

    • 创建一个 spring-mvc.xml 配置文件,用于配置 springmvc 的配置信息
    <!-- 组件扫描 -->
    <context:component-scanbase-package="cn.tedu.store.controller"/>
    
    <!-- 配置ViewResolver视图解析器 -->
    <bean
    	class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	<propertyname="prefix"value="/web/"/>
    	<propertyname="suffix"value=".jsp"/>
    </bean>
    
    <!-- 配置驱动,用于@ResponseBody的使用 -->
    <mvc:annotation-driven></mvc:annotation-driven>
    
    <!-- 其中还需要定义拦截器和其他的配置 -->
    

    • 创建一个 spring-dao.xml 配置文件,用于配置数据库的链接信息

    <!-- 组件扫描 -->
    <context:component-scanbase-package="cn.tedu.store.mapper"/>
    
    <!-- 加载db.properties,其中定义了数据库的配置信息 -->
    <util:propertiesid="dbConfig"location="classpath:db.properties"/>
    
    <!-- 数据源 -->
    <beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource">
    	<propertyname="url"value="#{dbConfig.url}"/>
    	<propertyname="driverClassName"value="#{dbConfig.driver}"/>
    	<propertyname="username"value="#{dbConfig.user}"/>
    	<propertyname="password"value="#{dbConfig.password}"/>
    	<propertyname="initialSize"value="#{dbConfig.initSize}"/>
    	<propertyname="maxActive"value="#{dbConfig.maxSize}"/>
    </bean>
    
    <!-- 配置MapperScannerConfigurer -->
    <beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">
    	<!-- 用于配置持久层接口在哪里,指定mapper的包,spring会自动为我们创建这个XXXMapper接口的对象,名称为类名首字母小写
    		-->
    	<propertyname="basePackage"value="cn.tedu.store.mapper"/>
    </bean>
    
    <!-- 配置SqlSessionFactoryBean -->
    
     	<beanclass="org.mybatis.spring.SqlSessionFactoryBean">
    
     		<!-- 用于配置数据库连接池 -->
    
     		<propertyname="dataSource"ref="dataSource"/>
    
     		<!-- 用于配置持久层映射文件在哪里,所有的xml文件,使用通配符 -->
    
     		<propertyname="mapperLocations"value="classpath:mappers/*.xml"/>
    
     	</bean>
    
  • 创建一个 spring-service.xml 配置文件,用于配置业务逻辑信息
<!-- 组件扫描 -->
<context:component-scanbase-package="cn.tedu.store.service"/>

<!-- 配置事务管理器 -->

  • 修改 web.xml 文件,添加前端控制器 DispatcherServlet 和解决中文乱码的过滤器
<!-- 配置中文乱码过滤器 -->
 <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>
</filter>

<filter-mapping>
	<filter-name>CharacterEncodingFilter</filter-name>
	<!-- 针对所有的路径都需要过滤 -->
	<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 配置前端控制器DispathcerServlet -->
<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:spring-*.xml</param-value>
	</init-param>
	<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
	<servlet-name>SpringMVC</servlet-name>
	<url-pattern>*.do</url-pattern>
</servlet-mapping>

文件约束

  • springspringmvc 配置文件的约束
<beansxmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
    
    
</beans>
  • XXXMapper.xml 的约束
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
 "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
原文  https://chenjiabing666.github.io/2018/05/09/搭建工程/
正文到此结束
Loading...