Eclipse下的SSM框架集成
Eclipse下的SSM框架集成
一:在eclipse下创建动态web工程ssmDemo_wanda并创建如下图所示的的目录和包,具体包的作用这里就不一一介绍了
二:在lib下加入ssm集成以及集成一些常用的功能jar并build classpath
附上整个整合jar包的下载地址:http://pan.baidu.com/s/1nvp0YfR
三:在config下创建数据源配置文件dbcp.properties以及日志文件log4j.properties
jdbc.properties
#oracle db.driver=oracle.jdbc.driver.OracleDriver db.url=jdbc:oracle:thin:@localhost:1521:ORCL db.user=test1 db.password=pypua #\u8FDE\u63A5\u6C60\u6700\u5C0F\u8FDE\u63A5\u6570 c3p0.minPoolSize =0 #\u8FDE\u63A5\u6C60\u6700\u5927\u8FDE\u63A5\u6570 c3p0.maxPoolSize =500 #\u91CD\u65B0\u83B7\u53D6\u65B0\u8FDE\u63A5\u65F6\u95F4 c3p0.timeout=1200 #\u5168\u5C40\u7F13\u5B58\u5927\u5C0F c3p0.max_statements=0 #\u67E5\u8BE2\u8FDE\u63A5\u6C60\u7A7A\u95F2\u8FDE\u63A5\u7684\u95F4\u9694 c3p0.idle_test_period =120 #\u8FDE\u63A5\u6C60\u4E2D\u8FDE\u63A5\u8017\u5C3D\u65F6\u4E00\u6B21\u83B7\u5F97\u7684\u8FDE\u63A5\u6570 c3p0.acquireIncrement =10 # c3p0.idleConnectionTestPeriod =120 #\u5B9A\u4E49\u6570\u636E\u5E93\u83B7\u53D6\u8FDE\u63A5\u5931\u8D25\u540E\u91CD\u590D\u5E38\u8BC6\u7684\u6B21\u6570 c3p0.acquireRetryAttempts =10
log4j.properties
### set log levels ### log4j.rootLogger = DEBUG, stdout , I , D, E log4j.additivity.org.apache = true ### \u8f93\u51fa\u5230\u63a7\u5236\u53f0 ### log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target = System.out log4j.appender.stdout.layout = org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss,SSS}(%r) [%-5p] [%t] --> %l:%m%n ### \u8f93\u51fa\u5230\u65e5\u5fd7\u6587\u4ef6 ### log4j.appender.I = org.apache.log4j.DailyRollingFileAppender log4j.appender.I.File = ${catalina.home}/logs/info.log log4j.appender.I.Append = true log4j.appender.I.DatePattern = '.'yyyy-MM-dd ## \u8f93\u51faINFO\u7ea7\u522b\u4ee5\u4e0a\u7684\u65e5\u5fd7 log4j.appender.I.Threshold = INFO log4j.appender.I.layout = org.apache.log4j.PatternLayout log4j.appender.I.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss,SSS}(%r) [%-5p] [%t] --> %l:%m%n ### \u8f93\u51fa\u5230\u65e5\u5fd7\u6587\u4ef6 ### log4j.appender.D = org.apache.log4j.DailyRollingFileAppender log4j.appender.D.File = ${catalina.home}/logs/debug.log log4j.appender.D.Append = true ## \u8f93\u51faDEBUG\u7ea7\u522b\u4ee5\u4e0a\u7684\u65e5\u5fd7 log4j.appender.D.Threshold = DEBUG log4j.appender.D.layout = org.apache.log4j.PatternLayout log4j.appender.D.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss,SSS}(%r) [%-5p] [%t] --> %l:%m%n ### \u4fdd\u5b58\u5f02\u5e38\u4fe1\u606f\u5230\u5355\u72ec\u6587\u4ef6 ### log4j.appender.E = org.apache.log4j.DailyRollingFileAppender ## \u5f02\u5e38\u65e5\u5fd7\u6587\u4ef6\u540d log4j.appender.E.File = ${catalina.home}/logs/error.log log4j.appender.E.Append = true ## \u53ea\u8f93\u51faERROR\u7ea7\u522b\u4ee5\u4e0a\u7684\u65e5\u5fd7 log4j.appender.E.Threshold = ERROR log4j.appender.E.layout = org.apache.log4j.PatternLayout log4j.appender.E.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss,SSS}(%r) [%-5p] [%t] --> %l:%m%n log4j.logger.com.crs.ticket=DEBUG log4j.logger.com.mchange=INFO log4j.logger.java.sql.ResultSet=INFO log4j.logger.org.apache.ibatis.logging.Log=INFO log4j.logger.java.sql.Connection=INFO log4j.logger.java.sql.Statement=INFO log4j.logger.java.sql.PreparedStatement=INFO
四:在config下创建spring-proploader.xml加载资源配置文件
spring-proploader.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:aop="http://www.springframework.org/schema/aop" 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-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd "> <!-- 配置加载资源文件 --> <context:property-placeholder location="classpath:config/jdbc.properties, classpath:config/redis.properties" /> </beans>
redis.properties配置文件(可根据项目需求选择性使用)
# redis normal group # \u4fdd\u6301\u7684\u6700\u5927\u6d3b\u8dc3\u8fde\u63a5\u6570 redis.maxActive=1000 # \u6700\u5927\u7684\u7a7a\u95f2\u8fde\u63a5\u6570 redis.maxIdle=500 # \u6700\u5927\u7684\u7b49\u5f85\u8fde\u63a5\u6570 redis.maxWait=2000 # \u6700\u5c0f\u7684\u7a7a\u95f2\u8fde\u63a5\u6570 redis.minIdle=3 # \u8fde\u63a5\u7528\u5b8c\u65f6\u7684\u52a8\u4f5c\uff0c0 \u5931\u8d25\uff0c1 \u963b\u585e\uff0c2 \u589e\u957f redis.whenExhaustedAction=1 #redis.host.1=111.204.247.2 redis.host=10.199.202.100 #redis.host.1=10.199.10.56 redis.port=6379 redis.db.share=0 redis.password=wanda2015\!
五:在config下创建mybatis配置文件spring-mybatis.xml及mybatis-configuration.xml
spring-mybatis.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:aop="http://www.springframework.org/schema/aop" 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-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd "> <!-- mybatis 配置 --> <!-- destroy-method="close"当数据库连接不使用的时候,就把该连接重新放到数据池中,方便下次使用调用. --> <bean id="myBatisDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <!-- 数据库驱动 --> <property name="driverClass" value="${db.driver}"></property> <!-- 数据库url --> <property name="jdbcUrl" value="${db.url}"></property> <!-- 数据库用户名 --> <property name="user" value="${db.user}"></property> <!-- 数据库密码 --> <property name="password" value="${db.password}"></property> <!-- 连接池中保留的最小连接数,默认为:3 --> <property name="minPoolSize" value="${c3p0.minPoolSize}" /> <!--连接池中保留的最大连接数。默认值: 15 --> <property name="maxPoolSize" value="${c3p0.maxPoolSize}" /> <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。默认值: 0 --> <property name="maxIdleTime" value="${c3p0.timeout}" /> <!-- 当连接池耗尽时候,一次获得连接数 --> <property name="acquireIncrement" value="${c3p0.acquireIncrement}" /> <!--c3p0全局的PreparedStatements缓存的大小。如果maxStatements与maxStatementsPerConnection均为0,则缓存不生效,只要有一个不为0,则语句的缓存就能生效。如果默认值: 0--> <property name="maxStatements" value="${c3p0.max_statements}" /> <!-- 当连接池连接耗尽时,客户端getConnection(),所等待的时间 --> <property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}" /> <!-- 当连接失效时,获得连接的次数,0为无限次(重连) --> <property name="acquireRetryAttempts" value="${c3p0.acquireRetryAttempts}" /> </bean> <!-- 配置session工厂 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="myBatisDataSource"></property> <property name="configLocation" value="classpath:config/mybatis-configuration.xml"></property> </bean> <!-- 配置mybatis Mapper扫描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.crs.**.mapper" /> </bean> <!-- 配置声明式事务管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="myBatisDataSource" /> </bean> <tx:advice id="userTxAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="del*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="find*" propagation="REQUIRED" /> <tx:method name="query*" propagation="REQUIRED" /> <tx:method name="get*" propagation="REQUIRED" /> <tx:method name="select*" propagation="REQUIRED" /> <tx:method name="list*" propagation="REQUIRED" /> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <!-- 配置spring AOP --><!-- expression="execution(* com.crs..service..*.*(..)))任意返回值的定义在service包以及子包中的任意方法,..代表参数 --> <aop:config> <aop:pointcut id="pc" expression="execution(* com.crs..service..*.*(..)))" /> <aop:advisor pointcut-ref="pc" advice-ref="userTxAdvice" /> </aop:config> </beans>
mybatis-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> <!-- 配置数据库方言 --> <properties> <!--property name="dialect" value="mysql"/--> <property name="dialect" value="oracle"/> </properties> <!-- 注入log4j --> <settings> <setting name="logImpl" value="LOG4J"/> </settings> <!-- 实现mybatis物理分页 --> <plugins> <plugin interceptor="com.crs.ticket.utils.mybatis.PaginationInterceptor"/> </plugins> </configuration>
实现mybatis物理分页的工具类PaginationInterceptor.java在这里不做详细配置展示,具体查看源代码
六:在config下创建springMVC配置文件spring-servlet.xml
spring-servlet.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:p="http://www.springframework.org/schema/p" 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 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <!-- 配置国际化支持 --> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <!-- 国际化信息所在的文件名 --> <property name="basename"> <value>messages/message</value> </property> <!-- 如果在国际化资源文件中找不到对应代码的信息,就用这个代码作为名称 --> <property name="useCodeAsDefaultMessage" value="true" /> </bean> <mvc:interceptors> <!-- 国际化操作拦截器 如果采用基于(请求/Session/Cookie)则必需配置 --> <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" /> </mvc:interceptors> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" /> <!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射 --> <mvc:annotation-driven /> <!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean --> <!-- 开启controller注解支持 --> <context:component-scan base-package="com.crs.**.web"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 --> <mvc:resources mapping="/images/**" location="/images/" /> <mvc:resources mapping="/css/**" location="/css/" /> <mvc:resources mapping="/js/**" location="/js/" /> <mvc:resources mapping="/resources/**" location="/resources/" /> <mvc:resources mapping="/upload/**" location="/upload/" /> <mvc:resources mapping="/script/**" location="/script/" /> <mvc:resources mapping="/style/**" location="/style/" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/view/" p:suffix=".jsp" /> <!-- 上传附件 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="utf-8" /> <!-- 自动解析action返回值 解析成json格式 --> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=utf-8</value> <value>text/plain;charset=utf-8</value> </list> </property> </bean> </beans>
七:在config下创建sprig配置文件spring-config.xml(重要)整合mybatis和springMVC
spring-config.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:aop="http://www.springframework.org/schema/aop" 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-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd "> <!-- spring配置文件 --> <!-- 引入资源配置文件 --> <import resource="spring-proploader.xml" /> <!-- 引入mybatis配置文件 --> <import resource="spring-mybatis.xml" /> <!-- 配置扫描注解beans --> <context:component-scan base-package="com.crs"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!--获取spring的application并在启动时注入 --> <bean id="applicationContextUtil" class="com.crs.ticket.utils.spring.ApplicationContextUtil"></bean> <!-- 引入Redis配置文件 --> <!-- <import resource="spring-redis.xml" />--> </beans>
spring-redis.xm(根据项目需要可选)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans default-lazy-init="false"><!-- 关闭懒加载 --> <bean> <!-- 保持的最大活跃连接数 --> <property name="maxActive" value="${redis.maxActive}" /> <!-- 最大的空闲连接数 --> <property name="maxIdle" value="${redis.maxIdle}" /> <!-- 最大的等待连接数 --> <property name="maxWait" value="${redis.maxWait}" /> <!-- 最小的空闲连接数 --> <property name="minIdle" value="${redis.minIdle}" /> <!-- 是否开启建立时验证,如果验证失败,将不加入队列 --> <property name="testOnBorrow" value="true" /> <!-- 是否开启放回队列时验证,如果验证失败,将不加入队列 --> <property name="testOnReturn" value="true" /> <!-- 是否开启等待状态转换到活动状态时验证,如果验证失败,将不加入队列 --> <property name="testWhileIdle" value="true" /> <!-- 连接用完时的动作,0 失败,1 阻塞,2 增长 --> <!-- <property name="whenExhaustedAction" value="${redis.whenExhaustedAction}" /> --> </bean> <bean name="jedisPool" id="jedisPool" class="redis.clients.jedis.JedisPool" singleton="true"> <constructor-arg> <ref bean="redisPoolConfig" /> </constructor-arg> <constructor-arg> <value>${redis.host}</value> </constructor-arg> <constructor-arg> <value>${redis.port}</value> </constructor-arg> <constructor-arg> <value>4000</value> </constructor-arg> <constructor-arg> <value>${redis.password}</value> </constructor-arg> </bean> <!-- 配置jedis客户端 --> <bean id="jedisClient" class="com.crs.ticket.utils.cache.JedisClient" singleton="true"> <property name="pool" ref="jedisPool" /> <property name="db" value="${redis.db.share}" /><!-- 建议一个项目使用一个db,防止数据混淆 --> </bean> <!-- 配置redis工具类 --> <bean id="redisUtil" class="com.crs.ticket.utils.cache.RedisUtil" singleton="true"> <property name="jedisClient" ref="redisClient" /> </bean> </beans>
八:配置web.xml(重要)
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_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>ssmDemo_wanda</display-name> <!-- 配置spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:config/spring-config.xml </param-value> </context-param> <!-- 配置监听类 --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <!-- log4j --> <context-param> <param-name>webAppRootKey</param-name> <param-value>gp.webapp.root</param-value> </context-param> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:config/log4j.properties</param-value> </context-param> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>60000</param-value> </context-param> <!-- spring mvc --> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- spring mvc character set --> <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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
九:创建DatabaseConnection.java类测试数据源连接情况
DatabaseConnection.java
package com.crs.ticket.wanda.test; import java.sql.Connection; import java.sql.SQLException; import oracle.jdbc.pool.OracleDataSource; public class DatabaseConnection { static private OracleDataSource ods; private DatabaseConnection() { System.out.println("zouni"); if(ods==null) { try { ods = new OracleDataSource(); ods.setURL("jdbc:oracle:thin:@localhost:1521:ORCL"); ods.setUser("test1"); ods.setPassword("pypua"); System.out.println("初始化。。wancheng"); } catch (SQLException ex) { } } } public static void main(String[] args) { if(ods==null) { new DatabaseConnection(); } Connection con=null; try { con = ods.getConnection(); System.out.println("数据库连接到"+con); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
十:创建一套完整SSM流程
1.使用mybatis逆向工程生成entity实体类以及mapper接口和xml
mybatis逆向工程下载地址:http://pan.baidu.com/s/1boZ6uJl,具体使用会在之后的博客中详细讲解,这里不做具体讲解
2.创建控制器conller类
TicketChooseController.java
package com.crs.ticket.wanda.ticket.web; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import com.crs.ticket.common.controller.BaseController; import com.crs.ticket.wanda.ticket.entity.Student; import com.crs.ticket.wanda.ticket.service.ticketChooseService; @Controller @Scope("prototype") public class TicketChooseController extends BaseController { @Autowired private ticketChooseService ticketchooseservice; @RequestMapping(value = "/ticket/choose/list",method = {RequestMethod.GET,RequestMethod.POST}) public ModelAndView initial(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "studentNo", required = false) String studentNo )throws Exception{ List<Student> studentList = ticketchooseservice.findStudentInfoByStudNo(studentNo); ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("studentList", studentList); modelAndView.setViewName("/ticket/choose/studentList"); return modelAndView; } }
3.创建服务接口service以及Impl
ticketChooseService.java
package com.crs.ticket.wanda.ticket.service; import java.util.List; import com.crs.ticket.common.service.BaseService; import com.crs.ticket.wanda.ticket.entity.Student; public interface ticketChooseService extends BaseService<Student> { List<Student> findStudentInfoByStudNo(String studentNo); }
ticketChooseServiceImpl.java
package com.crs.ticket.wanda.ticket.service.impl; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.crs.ticket.wanda.ticket.entity.Student; import com.crs.ticket.wanda.ticket.mapper.StudentMapper; import com.crs.ticket.wanda.ticket.service.ticketChooseService; @Service("TicketChooseService") public class ticketChooseServiceImpl implements ticketChooseService { @Resource private StudentMapper studentmapper; @Override public List<Student> findStudentInfoByStudNo(String studentNo) { Map<String, Object> parms = new HashMap<String, Object>(); parms.put("stuNo", studentNo); return studentmapper.findTicketInfo(parms); } }
4.编写mapper接口和xml文件
StudentMapper.java
package com.crs.ticket.wanda.ticket.mapper; import java.util.List; import java.util.Map; import org.apache.ibatis.annotations.Param; import com.crs.ticket.common.mapper.BaseMapper; import com.crs.ticket.wanda.ticket.entity.Student; public interface StudentMapper extends BaseMapper<Student> { List<Student> findTicketInfo(Map<String, Object> map); List<Student> findTicketByIdInfo(Map<String, Object> map); }
StudentMapper.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.crs.ticket.wanda.ticket.mapper.StudentMapper" > <resultMap id="BaseResultMap" type="com.crs.ticket.wanda.ticket.entity.Student" > <id column="STUNO" property="stuno" jdbcType="VARCHAR" /> <result column="CLASSID" property="classid" jdbcType="VARCHAR" /> <result column="SNAME" property="sname" jdbcType="VARCHAR" /> <result column="GREAD" property="gread" jdbcType="DECIMAL" /> <result column="DEPT" property="dept" jdbcType="VARCHAR" /> </resultMap> <sql id="Example_Where_Clause" > <where > <foreach collection="oredCriteria" item="criteria" separator="or" > <if test="criteria.valid" > <trim prefix="(" suffix=")" prefixOverrides="and" > <foreach collection="criteria.criteria" item="criterion" > <choose > <when test="criterion.noValue" > and ${criterion.condition} </when> <when test="criterion.singleValue" > and ${criterion.condition} #{criterion.value} </when> <when test="criterion.betweenValue" > and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} </when> <when test="criterion.listValue" > and ${criterion.condition} <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," > #{listItem} </foreach> </when> </choose> </foreach> </trim> </if> </foreach> </where> </sql> <sql id="Update_By_Example_Where_Clause" > <where > <foreach collection="example.oredCriteria" item="criteria" separator="or" > <if test="criteria.valid" > <trim prefix="(" suffix=")" prefixOverrides="and" > <foreach collection="criteria.criteria" item="criterion" > <choose > <when test="criterion.noValue" > and ${criterion.condition} </when> <when test="criterion.singleValue" > and ${criterion.condition} #{criterion.value} </when> <when test="criterion.betweenValue" > and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} </when> <when test="criterion.listValue" > and ${criterion.condition} <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," > #{listItem} </foreach> </when> </choose> </foreach> </trim> </if> </foreach> </where> </sql> <sql id="Base_Column_List" > STUNO, CLASSID, SNAME, GREAD, DEPT </sql> <select id="findTicketInfo" resultMap="BaseResultMap" parameterType="java.util.Map" > select stu.stuno "stuNo", stu.classid "classId", stu.sname "sName", stu.gread "gread", stu.dept "dept" from student stu where 1=1 <if test="stuNo != null and stuNo != ''"> AND stu.classid = #{stuNo,jdbcType=VARCHAR} </if> </select> <select id="findTicketByIdInfo" resultMap="BaseResultMap" parameterType="java.util.Map" > select stu.stuno "stuNo", stu.classid "classId", stu.sname "sName", stu.gread "gread", stu.dept "dept" from student stu where 1=1 <if test="stuNo != null and stuNo != ''"> AND stu.classid = #{stuNo,jdbcType=VARCHAR} </if> </select> </mapper>
5.编写index.jsp
index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <%--<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> --%> <title>跳转中</title> <%@ include file="/view/common/resource.jsp" %> <%--<meta http-equiv=refresh content="1;url=<%=basePath+"/index"%>"> --%> </head> <body> <c:set var="ctx" value="${pageContext.request.contextPath}" /> <h1>hello</h1> <form id="ticketForm" name="ticketForm" method="post" action="${ctx }/ticket/choose/list"> <input type="hidden" id="studentNo" name="studentNo" value="01"> </form> <button class="search" onclick="queryToStudent()" style="cursor:pointer;">搜索</button> </body> <script type="text/javascript"> function queryToStudent(){ alert("helloword"); ticketForm.submit(); } </script> </body> </html>
studentList.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <%@ include file="/view/common/resource.jsp" %> <%@ include file="/view/common/tags.jsp" %> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <h1>hahaha</h1> <table> <tbody> <tr> <td>学号 </td> <td>班级</td> <td>姓名</td> <td>成绩</td> <td>所属部门</td> </tr> <c:forEach items="${studentList }" var="student" varStatus="status"> <tr> <td>${student.stuno}</td> <td>${student.classid}</td> <td>${student.sname}</td> <td>${student.gread}</td> <td>${student.dept}</td> </tr> </c:forEach> </tbody> </table> </body> </html>
到此、ssm框架整合已经基本完成了、demo源码下载地址:http://pan.baidu.com/s/1min1nJE
划船不用桨、杨帆不等风、一生全靠浪