基于IDEA实现SSM整合框架的搭建配置流程
1、创建数据库表,以员工信息表为例子:
drop table if exists `em_info`;
create table `em_info` (
`em_id` int(50) not null auto_increment comment '员工id',
`em_name` varchar(100) not null comment '员工姓名',
`em_sex` varchar(30) not null comment '性别',
`em_birthday` date not null comment '出生日期',
`em_hiredate` date not null comment '入职日期',
`em_salary` double not null comment '工资',
primary key (`em_id`)
)engine=innodb default charset=utf8mb4;
insert into `em_info` values (null,'小李飞刀','男','1999-2-8','2019-6-6','9999');
2、创建maven工程
2.1创建module(web、service、dao、domain、utils子模块)
注意创建web子模块需要选择骨架,如下:
2.2、在domain子模块下创建实体类员工信息表em_info,并提供get和set方法
2.3、dao下创建iem_infodao接口,写查找方法findall()。需要连续两次快捷键alt+enter,添加依赖和导入,导包才消除红色不报错
2.4、同2.3步奏,service下创建iem_infoservice接口,写查找方法findall()。需要连续两次快捷键alt+enter,添加依赖和导入,导包才消除红色不报错。
2.5创建service实体类,实现接口,重写方法
2.6在web.mian包下手动创建java和resources两个包并指定为源码包和资源部
在resouces包下创建applicationcontext.xml和spring-mvc.xml文件
2.7applicationcontext.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: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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
</beans>
2.7.1、在添加下面配置扫描service和dao和配置连接池
<!-- 开启注解扫描,管理service和dao -->
<context:component-scan base-package="com.fzj.ssm.service">
</context:component-scan>
<context:component-scan base-package="com.fzj.ssm.dao">
</context:component-scan>
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置连接池 -->
<bean id="datasource" class="com.mchange.v2.c3p0.combopooleddatasource">
<property name="driverclass" value="${jdbc.driver}" />
<property name="jdbcurl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 把交给ioc管理 sqlsessionfactory -->
<bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean">
<property name="datasource" ref="datasource" />
2.7.2、加入配置事务
<!-- 配置spring的声明式事务管理 -->
<!-- 配置事务管理器 -->
<bean id="transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager">
<property name="datasource" ref="datasource"/>
</bean>
<tx:annotation-driven transaction-manager="transactionmanager"/>
2.8、sources下创db.properties
jdbc.driver=com.mysql.jdbc.driver
jdbc.url=jdbc:mysql://192.168.232.128:3306/ssm?useunicode=true&characterencoding=utf8
jdbc.username=root
jdbc.password=root
2.9、spring-mvc
加入约束头
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
</beans>
2.9.1、加入如下配置:
<!-- 扫描controller的注解,别的不扫描 -->
<context:component-scan base-package="com.itheima.ssm.controller">
</context:component-scan>
<!-- 配置视图解析器 -->
<bean id="viewresolver" class="org.springframework.web.servlet.view.internalresourceviewresolver">
<!-- jsp文件所在的目录 -->
<property name="prefix" value="/pages/" />
<!-- 文件的后缀名 -->
<property name="suffix" value=".jsp" />
</bean>
<!-- 设置静态资源不过滤 -->
<mvc:resources location="/css/" mapping="/css/**" />
<mvc:resources location="/img/" mapping="/img/**" />
<mvc:resources location="/js/" mapping="/js/**" />
<mvc:resources location="/plugins/" mapping="/plugins/**" />
<!-- 开启对springmvc注解的支持 -->
<mvc:annotation-driven />
<!--
支持aop的注解支持,aop底层使用代理技术
jdk动态代理,要求必须有接口
cglib代理,生成子类对象,proxy-target-class="true" 默认使用cglib的方式
-->
<aop:aspectj-autoproxy proxy-target-class="true"/>
3.0、web.xml相关配置
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemalocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!-- 配置加载类路径的配置文件 -->
<context-param>
<param-name>contextconfiglocation</param-name>
<param-value>classpath*:applicationcontext.xml</param-value>
</context-param>
<!-- 配置监听器 -->
<listener>
<listener-class>org.springframework.web.context.contextloaderlistener</listener-class>
</listener>
<!-- 配置监听器,监听request域对象的创建和销毁的 -->
<listener>
<listener-class>org.springframework.web.context.request.requestcontextlistener</listener-class>
</listener>
<!-- 前端控制器(加载classpath:springmvc.xml 服务器启动创建servlet) -->
<servlet>
<servlet-name>dispatcherservlet</servlet-name>
<servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>
<!-- 配置初始化参数,创建完dispatcherservlet对象,加载springmvc.xml配置文件 -->
<init-param>
<param-name>contextconfiglocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<!-- 服务器启动的时候,让dispatcherservlet对象创建 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherservlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 解决中文乱码过滤器 -->
<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>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
3.1、在web下面创建controller类(com.fzj.sss.controller.em_infocontroller)
编写findall()方法,把返回值list加入modelandview,
@controller
@requestmapping("/em_info")
public class em_infocontroller {
@autowired
private iem_infoservice em_infoservice;
@requestmapping("/findall.do")
public modelandview findall() throws exception{
modelandview mv =new modelandview();
list<em_info> em_infolist = em_infoservice.findall();
for (em_info em:em_infolist
) {
system.out.println(em.getem_name());
}
mv.addobject("em_infolist",em_infolist);
mv.setviewname("em_info");
return mv;
}
}
4、页面:
index.jsp 添加超连接
<a href="${pagecontext.request.contextpath}/em_info/findall.do">查询所有</a>
创建结果展示页面em_info.jsp
通过${em_infolist}el表达式获取值,并表格通过foreche循环展现数值
把项目web的war包部署到tomcat
运行结果:日期格式没有转换!因为目的证明ssm框架搭建成功而已
对比sqlyog结果: