目的:使用SSM将数据库的所有信息显示在jsp页面,成功证明SSM环境搭配成功。
前戏:导入32个包、建立config的source folder文件夹、里面有四个文件
1、在web.xml里面配置:
<!-- 配置监听器,上下文加载监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 指明applicationContext.xml文件位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置spring自带的字符编码过滤器 -->
<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>
<!-- 配置springmvc核心 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class>
<!-- 因为springmvc的配置文件不在默认位置,所以要指明 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
2、在applicationContext.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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 引入外部数据库连接 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置mapper扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.officn.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 配置sqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--配置数据源属性 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置mybatis文件在控制台打印sql语句 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- 配置pageHelper -->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<value>param1=value1</value>
</property>
</bean>
</array>
</property>
</bean>
<!-- 管理注解信息 -->
<context:annotation-config/>
<!-- 扫描指定包下配置了注解的类,除了controller包 -->
<context:component-scan base-package="com.officn.*">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 给业务层配置事务 -->
<tx:annotation-driven transaction-manager="txManager"/>
3、在springmvc-servlet.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
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置静态资源访问 -->
<mvc:default-servlet-handler />
<!-- 自动注入组件 -->
<mvc:annotation-driven conversion-service="conversionService" >
</mvc:annotation-driven>
<!-- 配置controller扫描 -->
<context:component-scan
base-package="com.offcn.house.controller" />
<!-- 配置视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 日期转换器 -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="com.offcn.house.converter.DateConverter"></bean>
</list>
</property>
</bean>
4、在mybatis-config.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="STDOUT_LOGGING" />
</settings>
</configuration>
5、在pojo类里写:
private int tid;
private String tname;
public int getTid() {
return tid;
}
public void setTid(int tid) {
this.tid = tid;
}
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
@Override
public String toString() {
return "Topic [tid=" + tid + ", tname=" + tname + "]";
}
6、在service接口里写:
List<Topic> findAllTopic();
7、在mapper接口里写:
List<Topic> findAllTopic();
8、在mapper.xml里面配置:
<mapper namespace="com.officn.mapper.TopicMapper">
<select id="findAllTopic" resultType="com.officn.pojo.Topic">
select * from topic
</select>
</mapper>
9、在serviceimpl类里写:
@Service("topicService")
public class TopicServiceImpl implements ITopicService {
@Resource
private TopicMapper topicMapper;
@Override
public List<Topic> findAllTopic() {
return topicMapper.findAllTopic();
}
}
10、在controller类里面写:
@Controller
public class TopicController {
@Resource
private ITopicService topicService;
@RequestMapping("/showTopic")
public String showTopic(HttpSession session){
List<Topic> list = topicService.findAllTopic();
session.setAttribute("list", list);
return "redirect:/index.jsp";
}
}
11、前端${list}接收,输入地址是http://localhost:8080/ssmenvironment/showTopic,数据出来了,成功。
12、需要资源的可以底下留言哈,以上配置可直接粘贴使用。。。