1 , create a database table to employee information table as an example:
TABLE em_info` the IF EXISTS `the DROP; the CREATE TABLE em_info`` ( `em_id` the INT (50) the AUTO_INCREMENT the COMMENT the NOT NULL 'employee ID', ` em_name` VARCHAR (100) the COMMENT the NOT NULL 'employee name', `em_sex` VARCHAR ( 30) NOT NULL COMMENT 'sex', `em_birthday` the NOT NULL dATE the COMMENT 'date of birth', ` em_hiredate` the NOT NULL dATE the COMMENT 'entry date', `em_salary` DOUBLE the NOT NULL the COMMENT 'wages', PRIMARY KEY (` em_id` ) ) = ENGINE INNODB the DEFAULT the CHARSET = utf8mb4; the INSERT the INTO `em_info` the VALUES (NULL," Li flying ',' M ',' 1999-2-8 ',' 2019-6-6 ',' 9999 ') ;
2 , create a maven project
2.1 Creating Module ( Web , Service , DAO , Domain , utils sub-module)
Note create web sub-module need to select the frame, as follows:
2.2 , in the domain to create the entity class employee information table under the sub-module Em_info , and provides get and set methods
2.3 , DAO created under IEm_infoDao interfaces, write the Find method findAll (). Need twice shortcut Alt + the Enter , add and import dependence, eliminate lead pack was not being given the red
2.4 , with 2.3 step-outs, Service created under IEm_infoService interfaces, write the Find method findAll (). Need twice shortcut Alt + the Enter , add and import dependence, eliminate lead pack was not being given the red.
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结果: