zoukankan      html  css  js  c++  java
  • 我的SpringMVC配置

    记住所有导的包都在org.springframework.web.servlet.mvc.annotation.下而不是

    org.springframework.web.protlet.mvc.annotation包下。这两个得看清楚!

    我的applicationContext.xml配置:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    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-2.5.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd ">
    <!-- 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
    <context:component-scan base-package="cn.jnti"></context:component-scan>
    <!-- 支持spring3.0新的mvc注解 -->
    <mvc:annotation-driven />
    <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->

    <!-- 注意导包时不要导成protlet,而是servlet包下 -->
    <bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
    <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
    <bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    p:suffix=".jsp"></bean>
    <bean

    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="cacheSeconds" value="0"></property>
    <property name="messageConverters">
    <list>
    <!-- spring使用jackson实现json输出 -->
    <bean
    class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
    </list>
    </property>
    </bean>
    <!-- 使用properties配置连接数据库 -->
    <bean
    class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
    <property name="locations">
    <list>
    <!-- 配置文件的位置 -->
    <value>classpath:jdbc.properties</value>
    </list>
    </property>
    </bean>

    <!-- 配置c3p0数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <!-- 指定JDBC驱动 -->
    <property name="driverClass" value="${driverClass}"></property>
    <property name="jdbcUrl" value="${jdbcUrl}"></property>
    <property name="user" value="${user}"></property>
    <property name="password" value="${password}"></property>
    </bean>
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="configLocation">
    <value>classpath:configuration.xml</value>
    </property>
    </bean>
    <bean id="template" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
    </bean>

    </beans>

    我的configuration.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>
    <settings>
    <setting name="logImpl" value="LOG4J" />
    </settings>
    <!-- <environments default="development"> <environment id="development">
    <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver"
    value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"
    /> <property name="username" value="scott" /> <property name="password" value="123"
    /> </dataSource> </environment> </environments> -->
    <mappers>
    <mapper resource="cn/jbit/bean/EmpMapper.xml" />
    </mappers>
    </configuration>

    我的EmpMapper:

    package cn.jnti.bean;

    import java.util.List;

    public interface EmpMapper {
    public List<Emp> selectAll();
    public Emp selectEmp(int empno);
    public List<Emp> selectEmpByEmp(Emp emp);
    }

    我的EmpMapper.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="cn.jnti.bean.EmpMapper">
    <cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true" />
    <select id="selectEmp" parameterType="cn.jnti.bean.Emp" resultType="cn.jnti.bean.Emp">
    select * from emp where empno= #{empno}
    </select>
    <select id="selectEmpByEmp" parameterType="cn.jnti.bean.Emp" resultType="cn.jnti.bean.Emp">
    select * from emp where 1=1
    <if test="empno !=null">
    and empno= #{empno}
    </if>
    <if test="ename !=null">
    and ename like #{ename}
    </if>
    </select>
    <select id="selectAll" resultType="cn.jnti.bean.Emp">
    select * from emp
    </select>
    </mapper>

    我的UserController:

    package cn.jnti.controller;

    import java.util.List;

    import javax.annotation.Resource;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;

    import cn.jnti.bean.Emp;
    import cn.jnti.service.EmpService;

    @Controller
    @RequestMapping("/user")
    public class UserController {

    @Resource(name="empservice")
    private EmpService service;

    @RequestMapping("/list")
    public ModelAndView getAllUser(){
    ModelAndView mav=new ModelAndView();
    mav.setViewName("/list");
    List<Emp> list=service.getAll();
    mav.addObject("list", list);
    return mav;
    }
    }

    我的EmpDao:

    package cn.jnti.dao;

    import java.util.List;

    import cn.jnti.bean.Emp;

    public interface EmpDao {
    List<Emp> getAll();
    Emp getById(Integer id);
    List<Emp> selectEmpByEmp(Emp emp);
    }

    我的EmpDaoImpl:

    package cn.jnti.dao;

    import java.util.List;

    import javax.annotation.Resource;

    import org.mybatis.spring.SqlSessionTemplate;
    import org.springframework.stereotype.Repository;

    import cn.jnti.bean.Emp;
    import cn.jnti.bean.EmpMapper;
    @Repository("empdao")
    public class EmpDaoImpl implements EmpDao {
    @Resource(name="template")
    private SqlSessionTemplate template;
    @Override
    public List<Emp> getAll() {
    EmpMapper mapper = template.getMapper(EmpMapper.class);
    return mapper.selectAll();
    }

    @Override
    public Emp getById(Integer id) {
    EmpMapper mapper = template.getMapper(EmpMapper.class);
    return mapper.selectEmp(id);
    }

    @Override
    public List<Emp> selectEmpByEmp(Emp emp) {
    EmpMapper mapper = template.getMapper(EmpMapper.class);
    return template.getMapper(EmpMapper.class).selectEmpByEmp(emp);
    }

    }

    我的EmpService:

    package cn.jnti.service;

    import java.util.List;

    import cn.jnti.bean.Emp;

    public interface EmpService {

    List<Emp> getAll();

    }

    我的EmpServiceImpl:

    package cn.jnti.service;

    import java.util.List;

    import javax.annotation.Resource;

    import org.springframework.stereotype.Service;

    import cn.jnti.bean.Emp;
    import cn.jnti.dao.EmpDao;
    @Service("empservice")
    public class EmpServiceImpl implements EmpService{
    @Resource(name="empdao")
    private EmpDao dao;
    @Override
    public List<Emp> getAll() {
    return dao.getAll();
    }

    }

    我的web.xml:

    <?xml version="1.0" encoding="UTF-8"?><!--
    <web-app version="3.0"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <display-name></display-name>
    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <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:applicationContext.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>
    </web-app>
    -->
    <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <display-name></display-name>
    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <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:applicationContext.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>
    </web-app>

    我的log4j.properties:

    log4j.rootLogger=DEBUG, Console


    #Console
    log4j.appender.Console=org.apache.log4j.ConsoleAppender
    log4j.appender.Console.layout=org.apache.log4j.PatternLayout
    log4j.appender.Console.layout.ConversionPattern=%d [%t] %u20105p [%c] u2010 %m%n
    log4j.logger.java.sql.ResultSet=INFO
    log4j.logger.org.apache=INFO
    log4j.logger.java.sql.Connection=DEBUG
    log4j.logger.java.sql.Statement=DEBUG
    log4j.logger.java.sql.PreparedStatement=DEBUG

    我的jdbc.properties:

    driverClass=oracle.jdbc.driver.OracleDriver
    jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl
    user=scott
    password=orcl

    最后是我用的jar包截图:

  • 相关阅读:
    使用超酷的jQuery缩略图生成插件NailThumb制作漂亮的缩略图web应用
    极客Web前端开发资源大荟萃#015
    极客编程小挑战#29:实现手机上常见的某一功能的效果显示
    在线制作gif loading图
    极客标签二重大礼华丽登场 宅男宅女必备套装
    SVG矢量图形课程基础入门篇
    浏览器JS交互,点击查询
    11选5专家投注研究算法
    大龙VBox API参数插件
    利用DLL劫持内存补丁技术注入
  • 原文地址:https://www.cnblogs.com/345214483-qq/p/4243163.html
Copyright © 2011-2022 走看看