eclipse 用maven搭建ssh框架
Maven + Spring + Spring MVC +Hibernate
首先
選擇Maven--》Maven Project
點選Next
點選Next
選中如圖所示,點選Next
將Group Id ,Artifact Id 填入Package可以手動清空(我填的Group Id:com.ssh02 , Artifact Id:ssh02);然後點選finish。、
剛建好的工程目錄如下圖:
這時候右擊專案名,選擇Build Path--》Configure Build Path
選中JRE System Library[J2EE 1.5]點選Remove,然後再點選Order and Export ,勾選Maven Dependencies
點選Apply 然後點選ok。工程目錄結構變成如下圖
右擊專案名,選擇Properties--》Project Facets
取消勾選Dynamic Web Module , java 改為1.8,點選右上角Runtimes 勾選tomcat 9.0(我用的是9.0) 。還是右擊專案名,選擇Properties--》Project Facets
此時 Dynamic Web Module版本變成了3.0,勾選它,然後會出現Further configuration avalilable
點選Further configuration avalilable,輸入如下圖
點選ok,Apply ,ok;工程目錄結構如下
整個工程版本,以及目錄檔案已經完全弄好,接著配置pom檔案,程式碼如下
<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>com.ssh02</groupId> <artifactId>ssh02</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>ssh Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <spring.version>4.3.5.RELEASE</spring.version> <hibernate.version>4.2.4.Final</hibernate.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-c3p0</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.2.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.40</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.2</version> </dependency> </dependencies> <build> <finalName>ssh02</finalName> </build> </project>
在src/main/resources建立如下圖檔案
JDBC.properties:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test_user?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=root
logback.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
spring-hibernate.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="byName"
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- 對web包中的所有類進行掃描,以完成Bean建立和自動依賴注入的功能 -->
<context:component-scan base-package="com.ssh">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 配置資料庫相關引數 -->
<context:property-placeholder location="classpath:JDBC.properties" ignore-unresolvable="true" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
</bean>
<!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.ssh.model</value>
</list>
</property>
</bean>
<!-- 配置事務管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<!-- 開啟註解事務-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
spring-mvc.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 對web包中的所有類進行掃描,以完成Bean建立和自動依賴注入的功能 -->
<context:component-scan base-package="com.ssh.controller" />
<!-- 開啟Springmvc註解模式 -->
<mvc:annotation-driven/>
<!-- 靜態資源預設servlet配置 -->
<mvc:default-servlet-handler/>
<!-- 配置jsp 顯示ViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
找到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"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
` http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ssh</display-name>
<!-- 配置spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-hibernate.xml</param-value>
</context-param>
<!-- 配置監聽器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置字元過濾器 -->
<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>
<!-- 配置Spring MVC Servlet -->
<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>
<welcome-file-list>
<welcome-file>/WEB-INF/views/login.jsp</welcome-file>
<welcome-file>/views/login.jsp</welcome-file>
<welcome-file>/login.jsp</welcome-file>
</welcome-file-list>
</web-app>
建立如下圖java程式碼目錄
UserController.java
package com.ssh.controller;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.ssh.model.User;
import com.ssh.service.UserService;
@Controller
public class UserController {
@Resource
private UserService userService;
@RequestMapping("/login")
public String login(String userName,String password){
User user=userService.findBy(userName, password);
if(user!=null){
return "seccued" ;
}else{
return "error";
}
}
}
UserDao.java
package com.ssh.dao;
import java.util.List;
import com.ssh.model.User;
public interface UserDao {
public void insert(String userName,String password);
public void delete(Integer id);
public List<User> findAll();
public User findBy(String userName,String password);
}
UserDaoImpl.java
package com.ssh.dao.impl;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.ssh.dao.UserDao;
import com.ssh.model.User;
@Repository
public class UserDaoImpl implements UserDao {
@Resource
private SessionFactory sessionFactory;
public void insert(String userName, String password) {
}
public void delete(Integer id) {
}
public List<User> findAll() {
return null;
}
@Transactional(readOnly=true)
public User findBy(String userName, String password) {
//hql寫法
/*String hql="from User where userName=? and password = ?";
Query query=sessionFactory.getCurrentSession().createQuery(hql);
query.setString(0, userName);
query.setString(1, password);
return (User) query.uniqueResult();*/
//sql寫法
String sql="select * from user where userName=? and password=?";
List<User> list=sessionFactory.getCurrentSession().createSQLQuery(sql).addEntity(User.class).setString(0, userName).setString(1, password).list();
User user=list.get(0);
return user;
}
}
User.java
package com.ssh.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Table(name="user")
@Entity
public class User {
@GeneratedValue(strategy=GenerationType.AUTO)
@Id
private Integer id;
private String userName;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
UserService.java
package com.ssh.service;
import java.util.List;
import com.ssh.model.User;
public interface UserService {
public List<User> findAll();
public void insert(String userName,String password);
public void delete(Integer id);
public User findBy(String userName,String password);
}
UserServiceImpl.java
package com.ssh.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.ssh.dao.UserDao;
import com.ssh.model.User;
import com.ssh.service.UserService;
@Service("UserService")
public class UserServiceImpl implements UserService{
@Resource
private UserDao userDao;
public List<User> findAll(){
return userDao.findAll();
}
public void insert(String userName, String password) {
userDao.insert(userName, password);
}
public void delete(Integer id) {
userDao.delete(id);
}
public User findBy(String userName, String password) {
return userDao.findBy(userName, password);
}
}
jsp程式碼不貼了(我懶了)
執行截圖
第一次寫,有不足的地方請大家加以糾正。