一、首先项目结构:
二、每个结构下的模板
1.pom文件常用的左边,若缺少可自行添加
坐标查询网址:https://mvnrepository.com/repos/central
<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>123</groupId>
<artifactId>123</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<!-- spring坐标 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.20.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.20.RELEASE</version>
</dependency>
<!-- spring核心坐标 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.20.RELEASE</version>
</dependency>
<!-- druid坐标 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
<!-- mysql坐标 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
<!-- <version>8.0.28</version> -->
</dependency>
<!-- spring jdbcTemplate 需要的jar -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.20.RELEASE</version>
</dependency>
<!-- 事务 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.20.RELEASE</version>
</dependency>
<!-- jsp -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- junit测试类 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- json jar -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
<!-- mybatis核心包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!-- mybatis整合spring包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
</project>
2.web.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>123</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!--servlet -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 这里的classpath:xxx.xml文件名要和项目中的一致-->
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--context -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 这里的classpath:xxx.xml文件名要和项目中的一致-->
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 字符编码过滤 -->
<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>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
3.springmvc.xml配置文件
注意:用时需要修改其中扫描包的地址和解析器的前后缀(根据不同的项目)!!!
<?xml version="1.0" encoding="UTF-8"?>
<!-- 命名空间 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://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
">
<!--扫描包 -->
<context:component-scan base-package="com.hp.controller"></context:component-scan>
<!-- 配置视图解析器 -->
<bean id="ViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 这里的前缀根据自己项目来决定 -->
<!-- <property name="prefix" value="/xxx/"></property> -->
<property name="prefix" value="/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:annotation-driven></mvc:annotation-driven>
<mvc:default-servlet-handler/>
</beans>
4.applicationContext.xml配置文件
注意:用时需要修改其中mapper包的路径!!!
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://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
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd
">
<!-- 扫描包 -->
<context:component-scan base-package="com.xx.mapper,com.xx.service"></context:component-scan>
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 读取配置文件-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${driverClassName}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${password}"></property>
</bean>
<!--配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 加载mybatis核心文件 -->
<property name="configLocation" value="classpath:sqlMapContig.xml"></property>
</bean>
<!--扫描mapper接口所在的包,为mapper创建代理实现类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xx.mapper"></property>
</bean>
<!-- 声明事物控制 -->
<!-- 平台事物管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事物增强 -->
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
5.sqlMapConfig.xml配置文件
注意:用时需要修改其中mapper包的路径!!!
<?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>
<package name="com.xx.bean"/>
</typeAliases>
<mappers>
<!--根据不同的的项目修改不同mapper包的路径-->
<package name="com.xx.mapper"/>
</mappers>
</configuration>
6.mapper.xml配置文件 !!!需要放在----->resources目录----->com.xx.mapper目录下
注意:代码中的配置均为示例,根据不同项目更改!!!
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 特别注意namespace属性后面的路径,一直要写到xxxMapper.java文件 -->
<mapper namespace="com.hp.mapper.RoomMapper">
<!-- 配置与数据库不同的字段 -->
<resultMap type="Room" id="romMap">
<!-- 根据不同项目中的实体类 -->
<id column="roomId" property="roomId"/>
<result column="typeId" property="typeId"/>
<association property="roomtype" javaType="roomtype">
<id column="typeId" property="typeId"/>
<result column="typeName" property="typeName"/>
</association>
</resultMap>
<!-- 每个方法对应的sql -->
<!-- 列表查询 -->
<select id="findRoom" resultMap="romMap">
SELECT r.*,t.typeName from room r,roomtype t where r.typeId=t.typeId
</select>
<!-- 添加 -->
<insert id="addRoom">
insert into room(roomName,typeId,area,price,descc) values (#{roomName},#{typeId},#{area},#{price},#{descc})
</insert>
<!-- 删除 -->
<delete id="deleteRoom" parameterType="int">
delete from room where roomId=#{roomId}
</delete>
<!-- 查询房间id -->
<select id="findById" resultType="roomtype">
SELECT * from roomtype
</select>
</mapper>
7.jdbc.properties配置文件
注意:代码中的配置均为示例,根据不同数据库更改!!!
driverClassName=com.mysql.jdbc.Driver
jdbc.username=root
password=123456
url=jdbc:mysql://localhost:3306/test
7.jsp文件
本项目jsp文件放在webapp---->view目录下
注意:
<script type="text/javascript" src="${pageContext.request.contextPath }/static/js/jquery-3.6.0.js"></script>
<script type="text/javascript">
这个静态资源如:js文件,bootStrap文件我是放在webapp-->static目录下,使用时静态资源地址若是有变化,及时更改!!!
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>
<script type="text/javascript" src="${pageContext.request.contextPath }/xxx/js/jquery-3.6.0.js"></script>
<script type="text/javascript">
</script>
</head>
<body>
</body>
</html>
好啦!!这就是我总结的一个项目的配置文件,为提供自己创建项目方便使用!!