springMVC+mybatis+mysql

SpringMVC+mybatis+mysql:

1.      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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>studyPoj</display-name>
  <welcome-file-list>
	<welcome-file>/index.jsp</welcome-file>
  </welcome-file-list>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/bean.xml</param-value>
  </context-param>
<!--  log4jConfigLocation:log4j配置文件存放路径  -->
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/conf/log4j.properties</param-value>
    </context-param>
    
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <servlet>
  <servlet-name>Log4jInit</servlet-name>
  <servlet-class>com.**.log.Log4jInit</servlet-class>
  <init-param>
   <param-name>log4j</param-name>
   <param-value>log4j.properties</param-value>
  </init-param>
 </servlet>
 
  <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>/*</url-pattern>
 </filter-mapping>

  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

   <!-- webservice i/f -->
  <servlet>
	<description>Apache CXF Endpoint</description>
    <servlet-name>cxf</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>cxf</servlet-name>
    <url-pattern>/ws/*</url-pattern>
  </servlet-mapping>
  
  <!-- error page -->
  <error-page>
	<exception-type>java.lang.Throwable</exception-type>
	<location>/errorpage/exception.do</location>
  </error-page>
  
  <error-page>
	<exception-code>404</exception-code>
	<location>/errorpage/exception.do</location>
  </error-page>
  
  <error-page>
	<exception-code>405</exception-code>
	<location>/errorpage/exception.do</location>
  </error-page>
  
  <error-page>
	<exception-code>415</exception-code>
	<location>/errorpage/exception.do</location>
  </error-page>
  
  <error-page>
	<exception-code>500</exception-code>
	<location>/errorpage/exception.do</location>
  </error-page>
</web-app>

2.       bean.xml

<?xml version="1.0"encoding="UTF-8"?>
<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: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-3.0.xsd
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context-3.0.xsd
                    http://www.springframework.org/schema/aop
                    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                    http://www.springframework.org/schema/tx
                     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
 
	<!--bean define-->
	<context:component-scan base-package="com" use-default-filters="false">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Component"></context:include-filter>
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"></context:include-filter>
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"></context:include-filter>
	</context:component-scan>
    <beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <propertyname="driverClassName" value="com.mysql.jdbc.Driver" />
        <propertyname="url"
            value="jdbc:mysql://localhost:3306/TBMS?useUnicode=true&characterEncoding=UTF-8"/>
        <propertyname="username" value="root" />
        <propertyname="password" value="root" />
    </bean>
 
    <beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation"value="classpath:spring/mybatis-config.xml"></property>
        <propertyname="dataSource" ref="dataSource" />
    </bean>
 
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!-- 使用annotation注解方式配置事务 -->
    <tx:annotation-driven transaction-manager="transactionManager"  />

</beans>

3.      mybatis-config.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPEconfiguration PUBLIC"-//mybatis.org//DTDConfig 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> 
<configuration>
    <typeAliases>
        <typeAliastype="com.**.entity.UserVO"alias="userVO"/>
    </typeAliases>
     <mappers>
        <mapperresource="com/**/dao/userMapper.xml"/>
   </mappers>
</configuration>

4.      userMapper.xml

<?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.**.dao.IUserDao">
    
   <select id="getUserTree" resultType="java.util.Map">
        select userNo,userName,userGroup from tb_user order by userGroup,userNo
    </select>
  
</mapper> 

5.      Log4jInit.java

import javax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importorg.apache.log4j.PropertyConfigurator;

publicclass Log4jInit extends HttpServlet {

    private static final long serialVersionUID= 1L;
    public void destroy() {
        super.destroy();
    }

 public Log4jInit() {
        super();
    }

    /**
     * Initialization of the servlet.<br>
     * @throws ServletException if an erroroccurs
     */

    public void init() throws ServletException{

        String file=this.getInitParameter("log4j");
        if(file != null) {
           PropertyConfigurator.configure(file);

        }
    }
}

6.      log4j.properties

### 设置Logger输出级别和输出目的地 ###

log4j.rootLogger=debug,stdout,logfile

 

### 把日志信息输出到控制台 ###

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout

 

### 把日志信息输出到文件:eci-ojt.log ###

log4j.appender.logfile=org.apache.log4j.FileAppender

log4j.appender.logfile.File=eci-ojt.log

log4j.appender.logfile.layout=org.apache.log4j.PatternLayout

log4j.appender.logfile.layout.ConversionPattern=[%d{HH\:mm\:ss\:SSS}][%p](%c\:%L) - %m%n

 

###显示SQL语句部分

log4j.logger.com.ibatis=DEBUG

log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG

log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG

log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG

log4j.logger.java.sql.Connection=DEBUG

log4j.logger.java.sql.Statement=DEBUG

log4j.logger.java.sql.PreparedStatement=DEBUG

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值