标签:www 年龄 生成 轻量 glib configure jdb play core
SSM(Spring+SpringMVC+MyBatis)框架集由Spring、SpringMVC、MyBatis三个开源框架整合而成,常作为数据源较简单的web项目的框架。
其中:
Spring是一个轻量级的控制反转(IOC)和面向切面(AOP)的容器框架。
SpringMVC分离了模型对象(Model)、视图(View)、控制器(Controller),这种分离让它们更容易进行定制。
MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架。
今天,就带领大家见证基础,进行SSM的学习。
在此之前呢,首先要引入的是系统分层,如何分层呢?
表示层(UI):数据展现 / 操作界面,请求分发(总所周知的MVC 是表示层的架构思想)。
持久层(服务层):封装业务逻辑处理。
持久层(数据访问层):封装数据访问逻辑。
而各层之间的关系呢,表示层通过接口调用业务层,业务层通过接口调用持久层。之所以引入系统分层的概念,主要的原因是:当下一层的实现发生改变的时候,并不会影响下一层。
叙述的就是这些了,开始我们的项目了
首先创建一个maven项目,格局如下:
1.pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ninemax</groupId> <artifactId>springMVC-test2</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.2.RELEASE</version> </dependency> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc14</artifactId> <version>10.2.0.3.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.5.RELEASE</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.8</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.2</version> </dependency> </dependencies> </project>
2.User.java
package com.nine.entity; public class User { private String name; private Integer age; private String gender; private Double salary; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public Double getSalary() { return salary; } public void setSalary(Double salary) { this.salary = salary; } @Override public String toString() { return "User [name=" + name + ", age=" + age + ", gender=" + gender + ", salary=" + salary + "]"; } }
3.UserDao.java
package com.nine.dao; import java.util.List; import com.nine.entity.User; public interface UserDao { List<User> findAll(); }
4.UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <mapper namespace="com.nine.dao.UserDao"> <select id="findAll" resultType="com.nine.entity.User"> SELECT * FROM T_USER </select> </mapper>
5.UserService.java
package com.nine.service; import java.util.List; import com.nine.entity.User; public interface UserService { List<User> userList(); }
6.UserServiceImpl
package com.nine.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.nine.dao.UserDao; import com.nine.entity.User; @Service public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; public List<User> userList() { List<User> list = userDao.findAll(); return list; } }
7.UserController
package com.nine.controller; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.nine.entity.User; import com.nine.service.UserService; @Controller public class UserController { @Autowired private UserService userService; @RequestMapping("/hello") public String hello(HttpServletRequest request) { List<User> list = new ArrayList<User>(); try { list = userService.userList(); } catch (Exception e) { e.printStackTrace(); return "error"; } request.setAttribute("list", list); return "hello"; } }
8.application.properties
spring.datasource.url=jdbc:oracle:thin:@10.21*.4.2*:1521:orcl spring.datasource.username=* spring.datasource.password=* spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
9.spring-mvc.xml (这是springMVC重要的配置文件,前往不能忽略)
<?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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <!-- 配置组件扫描 --> <context:component-scan base-package="com.nine"/> <!-- 配置MVC注解扫描 --> <mvc:annotation-driven/> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/"/> <property name="suffix" value=".jsp"/> </bean> <util:properties id="application" location="classpath:application.properties"/> <bean id="ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="#{application[‘spring.datasource.driver-class-name‘]}"/> <property name="url" value="#{application[‘spring.datasource.url‘]}"/> <property name="username" value="#{application[‘spring.datasource.username‘]}"/> <property name="password" value="#{application[‘spring.datasource.password‘]}"/> </bean> <!-- 配置SqlSessionFactoryBean --> <!-- 该bean的作用是用来代替MyBatis配置文件 --> <bean class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 指定连接池 --> <property name="dataSource" ref="ds"/> <!-- 指定映射文件位置 --> <property name="mapperLocations" value="classpath:com/nine/dao/*.xml"/> </bean> <!-- 配置MapperScannerConfigurer --> <!-- 该bean会扫描指定包及其子包下面的所有的Mapper映射器(接口),然后生成符合该接口要求的对象 (通过调用SqlSession的getMapper方法),接下来,会将这些对象(即Mapper映射器的实现对象)添加 到Spring容器里面(默认的id是首字母小写之后的接口名) --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 指定要扫描的包 --> <property name="basePackage" value="com.nine.dao"/> </bean> </beans>
10.记住了!还有一个地方 web.xml,不要忘了。
<?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" version="2.5"> <display-name>springMVC-test2</display-name> <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:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
接下来就是简单的一些静态页面了,这里我没有做太好的描述,只有一些简单的页面供大家参考
hello.jsp
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ page import="java.util.*" language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <html> <head> <title>list</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="stylesheet" type="text/css" href="css/style.css" /> </head> <body> <h1> 列表 </h1> <table border="1px" cellspacing="0px" style="border-collapse:collapse"> <tr> <td> 姓名 </td> <td> 年龄 </td> <td> 性别 </td> <td> 薪资 </td> </tr> <c:forEach items="${list}" var="e" varStatus="s"> <tr> <td>${e.name}</td> <td>${e.age }</td> <td>${e.gender }</td> <td>${e.salary}</td> </tr> </c:forEach> </table> </body> </html>
error.jsp
<%@page pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<html>
<head></head>
<body style="font-size:30px;">
系统繁忙,稍后重试
</body>
</html>
做到这里,基本上就算做完了,不过首先呢,在完成了之后,最好是先做一个测试类,看是否能够找出结果
UserTest.java
package test; import java.util.List; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.nine.dao.UserDao; import com.nine.entity.User; public class UserTest { @Test public void test1() { @SuppressWarnings("resource") ApplicationContext ac = new ClassPathXmlApplicationContext("spring-mvc.xml"); UserDao dao = ac.getBean("userDao", UserDao.class); List<User> list = dao.findAll(); System.out.println(list); } }
然后运行这个测试类,看是否正确,如果在控制台出现结果,恭喜你你已经完成了!
然后运行Tomcat,输入http://localhost:8081/springMVC-test2/hello
不知道大家完成的怎么样了,如果出现问题,可以在下面进行留言,我会为大家进行解答.
springMVC+spring+MyBatis(SSM)的简单配置
标签:www 年龄 生成 轻量 glib configure jdb play core
原文地址:http://www.cnblogs.com/baiyp/p/7865827.html