IDEA POM文件整理好之后配置xml文件---不完整

xml文件需放在resources文件里,创建步骤如图

 

 选resources属性

 

resources下面创建3个文件夹

 resoureces下创建3个文件夹,WEB-INF下创建3个文件夹

 web下controller包

 

 

 

web.xml

 

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<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">
<!--  web.xml详解  https://blog.csdn.net/believejava/article/details/43229361-->
    <!--web项目会首先读取context-param和listener节点-->

    <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>
    <!--    紧接着,创建一个容器ServletContext(Application),在web项目所有部分共享这个上下文-->
<!--    接着,容器会读取<filter></filter>,根据指定的类路径来实例化过滤器。-->

    <!--    springmvc前端控制器-->
    <servlet>
        <servlet-name>taotao-manager</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--  contextConfigLocation不是必须的,如果不配置contextConfigLocation,Springmvc的配置文件默认-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/springmvc.xml</param-value>
        </init-param>
        <!-- 在web应用程序启动的时候就加载这个servlet,(实例化并调用其init()方法-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>taotao-manager</servlet-name>
        <!--    会拦截所有请求包括静态资源,需要在springmvc.xml中添加静态资源的映射-->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!--  解决乱码问题-->
    <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>


    <display-name>Archetype Created Web Application</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>






</web-app>

 

 

 

 

 

applicationContext-dao.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"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"

       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx4.2.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-4.2.xsd">

    <!--加载配置文件-->
    <context:property-placeholder location="classpath:resourece/*.properties"/>

    <!--配置数据库连接池,使用druid -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
          destroy-method="close">
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="maxActive" value="10"/>
        <property name="minIdle" value="5"/>
    </bean>
    <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--数据库连接池-->
        <property name="dataSource" ref="dataSource"/>
        <!--加载Mybatis全局配置文件-->
        <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/>
    </bean>
    <!--    配置扫描包,加载mapper代理对象-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 扫描dao层接口包路径,注册代理类到IOC中;如果需要扫描多个包中间用半角逗号隔开 -->
        <property name="basePackage" value="com.taotao.mapper"/>
    </bean>

</beans>

 

db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.44.134:3306/2022?characterEncoding=utf-8
jdbc.username=root
jdbc.password=Root2002.



 

 

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>

</configuration>

 

applicationContext-service.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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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/mvc
       https://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan  base-package="com.taotao.service" />
</beans>

 

applicationContext-trans.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: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/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    <!--设置事务管理器,引用的是applicationContext-dao.xml中配置的datasource-->
        <bean id="transactionMabager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <!--            数据源-->
            <property name="dataSource" ref="dataSource"/>
        </bean>

    <!--            配置事务通知,引用上面设置的事务管理器
                    add,delete,update开头的方法需要事务
                    其他方法都是只读的-->

    <tx:advice id="advice" transaction-manager="transactionMabager">
        <tx:attributes>
            <tx:method name="create*" propagation="REQUIRED"/>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="updata*" propagation="REQUIRED"/>
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="insert*" propagation="REQUIRED"/>
            <tx:method name="*" propagation="SUPPORTS" read-only="true"/>
<!--            <tx:method name="*"  read-only="true"/>-->
        </tx:attributes>
    </tx:advice>
    <!--定义连接点,advisor定义了通知在连接点上执行即植入-->
    <aop:config>
        <aop:pointcut id="pointcut" expression="execution(* com.taotao.service.*.*(..))"/>
        <aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>
    </aop:config>
</beans>

 

spring下创建springmvc.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:mvc="http://www.springframework.org/schema/mvc"
       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/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

     <mvc:annotation-driven/>
<!--    开启扫描注解-->
    <context:component-scan base-package="com.taotao.controller"/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <!--    资源映射-->
    <mvc:resources  location="/WEB-INF/css" mapping="*/css/**" />
    <mvc:resources  location="/WEB-INF/js" mapping="*/js/**" />


</beans>

 

index.jsp

<html>
<body>
重定向到WEB-INF下面
<jsp:forward page="WEB-INF/index.jsp"></jsp:forward>
</body>
</html>

WEB-INF下面index.jsp

<html>
<body>
<h2>WEB-INF ,,show me!!!!!!!!!!!!!!!!!!!!!!</h2>

</body>
</html>

web下controller包下面pageController

@Controller
public class PageController {
/**
 * 打开首页
 */

    @RequestMapping("/")
    public String showIndex(){
        return "index";
    }

}

 

 

远程读取MySql username

https://github.com/alibaba/druid/wiki/%E4%BD%BF%E7%94%A8ConfigFilter

 

posted @ 2022-03-27 23:32  dune.F  阅读(255)  评论(0编辑  收藏  举报