第一步添加jar包 在pom.xml文件中进行添加
- 单元测试的包
- Mysql的驱动程序包
- Mybatis的依赖包
- spring-context的核心包
- mybatis-spring的整合包
- spring-aspects的包
- spring-jdbc的包
- spring-test的包
- spring-tx的包(处理事务的)
- spring-web包
- spring-webmvc包
- 日志包
- javax-annotation的包(是使用@resource这个注解的 类似AutoWired)
- jstl的包
- jackjson的包
- servlet和jsp的包
- druid包
- 添加一个这个
<build> <resources> <resource> <directory>src/main/java</directory><!--所在目录--> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build>
二.在web.xml文件中配置
<?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">
<!--注册一个前端控制器-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<!-- 因为springmvc中静态访问会出现问题所以暂时先使用*.action-->
<servlet-name>DispatcherServlet</servlet-name>
<!--<url-pattern>/</url-pattern>-->
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!--配置post提交的中文乱码问题的过滤器-->
<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>
<!--初始化Spring的容器文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<!--使用监听器监听Spring容器文件-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
三,配置SpringMVC的容器文件 在resources下面创建一个springmvc.xml文件
什么是SpringMVC
Spring MVC是基于Java实现MVC的轻量级Web框架
<?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">
<!--配置注解开发的包扫描器-->
<context:component-scan base-package="com.bjpowernode.controller"/>
<!--配置注解驱动-->
<!-- 配置注解驱动的作用
1)代替处理器映射器和处理器适配器的配置
2)对json数据进行响应支持
3)可以引用日期转换器服务-->
<mvc:annotation-driven/>
<!--解决无法访问静态资源的:直接交给tomcat服务器处理-->
<mvc:default-servlet-handler/>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--配置视图的前缀名-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--配置视图的后缀名-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
四.在resources下面创建一个jdbc文件夹 在下面创建一个db.properties这个文件去配置本地mysql数据库的基本参数 和连接池的参数
##配置本地mysql数据的基本参数
jdbc_driver = com.mysql.cj.jdbc.Driver
jdbc_url = jdbc:mysql://localhost:3306/day13?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
jdbc_user = root
jdbc_password = root
##连接池的参数
##初始化连接数
initialSize=10
##最小空闲数
minIdle=10
##最大连接数
maxActive=20
##最大的等待时间
maxWait=6000
五 Mybatis的创建
1.在resources下创建mybatis文件夹 然后创建SqlMapConfig.xml 文件保持空即可
<?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>
<!--保持数据为空即可,这里面的操作都交给Spring容器完成-->
</configuration>
六 然后在resources下创建spring文件然后创建
applicationContext-dao
有没有想过为什么要写db.properties?
因为配置文件配置好了我们一般不会去随意的修改,如果把数据库的配置参数直接写进bean中,如果后期数据库发生了变化,密码发生了变化是不是要去修改bean中的参数了,这样就不利于代码的维护了,放在属性文件中我只需要更新属性文件即可,其他的就可以不用再进行编译等操作了
<?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">
<!--1、加载jdbc的属性文件:db.properties-->
<context:property-placeholder location="classpath:jdbc/db.properties"/>
<!--2、使用bean管理mysql数据库连接池技术-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!--2.1:配置mysql的基本参数-->
<property name="driverClassName" value="${jdbc_driver}"/>
<property name="url" value="${jdbc_url}"/>
<property name="username" value="${jdbc_user}"/>
<property name="password" value="${jdbc_password}"/>
<!--2.2:配置连接池的基本参数-->
<property name="initialSize" value="${initialSize}"/>
<property name="minIdle" value="${minIdle}"/>
<property name="maxActive" value="${maxActive}"/>
<property name="maxWait" value="${maxWait}"/>
</bean>
<!--3、使用bean管理mybatis的工厂对象-->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--3.1加载数据库的数据源-->
<property name="dataSource" ref="dataSource"/>
<!--3.2加载mybatis的全局配置文件-->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/>
<!--3.3配置pojo类的别名包扫描器-->
<property name="typeAliasesPackage" value="com.bjpowernode.pojo"/>
</bean>
<!--4、使用bean管理mybatis动态代理的映射-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--提供mybatis动态映射的包-->
<property name="basePackage" value="com.bjpowernode.mapper"/>
</bean>
<!--bean管理学生的创建-->
</beans>
applicationContext-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">
<!--配置注解开发的包扫描器-->
<context:component-scan base-package="com.bjpowernode.service"/>
</beans>
applicationContext-trans(事务管理) 关键要检查约束全不全
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--1、配置平台事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--2、配置事务的定义信息-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" isolation="SERIALIZABLE" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--3、配置事务和切点之间的关系-->
<aop:config>
<aop:pointcut id="txService" expression="execution(* com.bjpowernode.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txService"/>
</aop:config>
</beans>
然后把jQuery导一下在webapp创建一个js文件夹把jquery放进去