在学习了ssm的三大框架后,终于来到了对ssm的三大框架的整合,以便我们熟练使用这三大框架
步骤:
- 在eclipse中创建一个Web项目
- 在WebContent------WEB-INF--------lib下导入包,并添加依赖
3.配置Web.xml:(display-name:项目的名字)
<?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_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>smbmsexe</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>login.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- spring配置文件默认存放在web-inf下 -->
<!-- 配置監聽器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置過濾器 -->
<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>
<!-- 配置DispatcherServlet -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
- 在src下创建两个xml文件,一个是spring.xml,一个是springmvc.xml
配置spring.xml:
<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"
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-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 配置连接池 -->
<bean class="org.apache.commons.dbcp.BasicDataSource"
id="dataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver"></property>
<!--smbms为数据库-->
<property name="url" value="jdbc:mysql:///smbms"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!-- 配置SqlSessionFactoryBeanName -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 配置mybatis分页 -->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties" value="helperDialect=mysql"></property>
</bean>
</array>
</property>
</bean>
<!-- Mapper扫描接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.offcn.smbms.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"></property>
</bean>
<!-- 开启扫描 -->
<context:component-scan base-package="com.offcn.smbms.service"></context:component-scan>
</beans>
配置spingmvc文件:
<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">
<!-- 扫描controller -->
<context:component-scan base-package="com.offcn.smbms.controller"></context:component-scan>
<mvc:annotation-driven />
<mvc:default-servlet-handler/>
<bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value ="/"></property>
<property name="suffix" value =".jsp"></property>
</bean>
<mvc:annotation-driven conversion-service="convertService"/>
<!-- 配置转换器 -->
<bean class ="org.springframework.format.support.FormattingConversionServiceFactoryBean" id = "convertService">
<!-- 注入 -->
<property name="converters">
<list>
<bean class="com.offcn.smbms.controller.DataConvert"></bean>
</list>
</property>
</bean>
</beans>
5.在src下创建四个包,一个pojo包,一个是mapper包,一个是service包,一个是service的实现类包:
在pojo包:创建一个与数据库表中的属性一样的变量
在mapper包:创建接口,该接口用来写sql语句
public interface BillMapper {
//查询所有的账单记录
@Select("<script>"
+ "select * from smbms_bill"
+ "<where>"
+ "<if test=\"productName !=null and productName !=''\">and productName = #{productName}</if>"
+ "<if test=\"providerId>0 \"> and providerId=#{providerId} </if>"
+ "<if test=\"isPayment>=0 \"> and isPayment = #{isPayment}</if>"
+ "</where>"
+ "</script>")
@Results(value = {
@Result(property="provider",column="providerId",one=@One(select="com.offcn.smbms.mapper.ProviderMapper.findProvideId"))
})
public List<Bill> blist(@Param("productName")String productName,@Param("providerId")int providerId,@Param("isPayment")int isPayment);
}
在service包:创建接口
public interface BillService {
//查询所有的账单的记录
public List<Bill> blist(String productName,int providerId,int isPayment);
}
在service实现类包中,在类的开头添加注解@Service(“billService”),该billservice与controller中创建的变量相同。
@Service("billService")
public class BillServiceImpl implements BillService{
@Autowired
private BillMapper billMapper;
@Override
public List<Bill> blist(String productName, int providerId, int isPayment) {
// TODO Auto-generated method stub
return billMapper.blist(productName, providerId, isPayment);
}
}
controller包中创建类:
@Controller
public class BillController {
@Autowired
private BillService billService;
//查询所有的记录
@RequestMapping("/billList")
@ResponseBody
public PageInfo<Bill> billList(@RequestParam(value="productName")String productName, @RequestParam(value="providerId",defaultValue="0",required=true)int providerId, @RequestParam(value="isPayment",defaultValue="-1")int isPayment,
@RequestParam(name="pageNum",defaultValue="1",required=true)int pageNum,HttpSession session){
System.out.println("123er4");
PageHelper.startPage(pageNum, 4);
List<Bill> blist = billService.blist(productName, providerId, isPayment);
session.setAttribute("blist", blist);
PageInfo<Bill> pageInfo = new PageInfo<Bill>(blist);
return pageInfo;
}
}