基于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结果: