1、使用IntelliJ IDEA 建立Maven管理的JAVA Web項目html
①新建項目,選擇Maven Modulejava
在本地磁盤用戶的。.m2目錄下生成settings文件:web
Maven工程搭建完畢!!!spring
② 開發步驟:數據庫
自動從本地倉庫下載Jar包:apache
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.wj</groupId> <artifactId>SpringMVC_SSM</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>SpringMVC_SSM Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <!--junit配置--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency>
<!--javaee 支持--> <dependency> <groupId>javaee</groupId> <artifactId>javaee-api</artifactId> <version>5</version> </dependency>
<!--spring+Spring mvc--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.1.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.1.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.2.3.release</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.1.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.2.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.9.RELEASE</version> </dependency>
<!--MyBatisjar包--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.2</version> </dependency>
<!--Mybatis整合Spring jar包--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.17.1-GA</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency>
<!--驅動jar包--> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>11.2.0.1.0</version> </dependency> </dependencies> <build> <finalName>SpringMVC_SSM</finalName> </build> </project>
③ 因爲Maven對oracle數據庫的不支持,咱們須要將Oracle裝載到本地倉庫,api
讓Maven使用Oraclesession
一、配置Maven3.3.9 bin到環境變量mybatis
而後能夠經過cmd命令行 mvn -version測試有沒有測試成功!!!架構
二、定位到Oracle安裝目錄的jdbc文件夾
④搭建分層架構---實現添加學生
實體類UserInfo:
package cn.wj.entity; /** * Created by WJ on 2017/1/16. */ public class UserInfo { private Integer id; private String name; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } 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; } }
dao接口: 無需dao實現類,經過代理生成
public interface IUserInfoDao { public void add(UserInfo info); }
service層:
public interface IUserInfoService { public void add(UserInfo info); }
service實現類:
public class UserInfoServiceImpl implements IUserInfoService { private IUserInfoDao dao; public void add(UserInfo info) { dao.add(info); } public IUserInfoDao getDao() { return dao; } public void setDao(IUserInfoDao dao) { this.dao = dao; } }
控制器類:controller
public class UserInfoController implements Controller { private IUserInfoService service; public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { String uname=httpServletRequest.getParameter("uname"); Integer uage=Integer.valueOf(httpServletRequest.getParameter("uage")); UserInfo info=new UserInfo(); info.setName(uname); info.setAge(uage); service.add(info); return new ModelAndView("/welcome.jsp"); } public IUserInfoService getService() { return service; } public void setService(IUserInfoService service) { this.service = service; } }
因爲是整合MyBatis,須要書寫核心配置文件:
小配置:UserInfoDao.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="cn.wj.dao.IUserInfoDao"> <select id="add" parameterType="UserInfo"> insert into userinfo values(SEQ_Num.nextval,#{name},#{age}) </select> </mapper>
MyBatis配置文件:mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <package name="cn.wj.entity"/> </typeAliases> <mappers> <package name="cn.wj.dao"/> </mappers> </configuration>
applicationContext.xml:進行植入dao、service、controller、事務管理器、數據源、sessionFactory
index.jsp頁面:
<body> <form action="${pageContext.request.contextPath }/userAction.do" method="post"> 用戶名:<input name="uname"/> <br/><br/> 年齡:<input name="uage"/><br/><br/> <input type="submit" value="add"/> </form> </body>
Web.xml:進行配置中央調度器
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <!-- 1.針對Spring配置:讀取配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 註冊ServletContext監聽器,建立容器對象,而且將ApplicationContext對象放到Application域中 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 針對SpringMVC的配置::::::中央調度器:本質上一個serlvet 配置的關於SpringmVC組件 --> <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:applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
實現效果:
注:若出現此類異常--org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
解決方案:參見博客
http://www.cnblogs.com/jstarseven/p/5803697.html