1. 程式人生 > >使用maven搭建ssm環境進行登入

使用maven搭建ssm環境進行登入

eclipse有預設的maven,但是我是使用自己下載的:http://maven.apache.org/download.cgi

GitHub:https://github.com/SUBEYZ/HelloGit/tree/master/blankMavenSSM

下載好設定setting.xml

配置:

<localRepository>F:/maven倉庫/respository</localRepository>
 <mirror>
 	 <id>alimaven</id> 
 	 <name>aliyun maven</name> 
 	 <url>http://maven.aliyun.com/nexus/content/groups/public/</url> 
 	 <mirrorOf>
 	 	central
 	 	</mirrorOf> 
 	 	</mirror>

  </mirrors>
		<profile>
			<id>jdk-1.8</id>
			<activation>
				<activeByDefault>true</activeByDefault>
				<jdk>1.8</jdk>
			</activation>
			<properties>
				<maven.compiler.source>1.8</maven.compiler.source>
				<maven.compiler.target>1.8</maven.compiler.target>
				<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
			</properties>
		</profile>

接下來配置eclipse

然後就可以建立maven專案了,file--new maven project

勾選第一個框框,建立空的就好了

 

我的理解:group id是公司、組織名字,artifact id 是專案名字,version是版本號,packaging是這個專案打包為什麼型別

有三種:jar--Javase,war--Javaweb專案,pom--分散式父專案,這裡選war

建立好之後報錯了,因為maven沒有相容沒有web.xml的環境,就給他配一個就OK了,之後還有東西要寫

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee                       
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
</web-app>

然後寫pom.xml,在這裡寫完一儲存,maven立馬就會到指定的路徑去下載相應jar

依賴包在這裡找:https://mvnrepository.com/

<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>bl</groupId>
  <artifactId>blankMavenSSM</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  	<!-- 版本控制 -->
  	<properties>
		<servlet-version>3.0.1</servlet-version>
		<jsp-version>2.2</jsp-version>
		<jstl-version>1.2</jstl-version>
		<spring-version>4.1.6.RELEASE</spring-version>
		<aspectjweaver-version>1.8.6</aspectjweaver-version>
		<mybatis-version>3.2.7</mybatis-version>
		<mybatis-spring-version>1.2.3</mybatis-spring-version>
		<log4j-version>1.2.17</log4j-version>
		<mysql-connector-java-version>5.1.38</mysql-connector-java-version>
		<jackson-version>2.4.1</jackson-version>
		<commons-fileupload-version>1.3.1</commons-fileupload-version>
	</properties>
	
	<!-- 依賴 -->
  	<dependencies>
		<dependency> 
		    <groupId>javax.servlet</groupId>
		    <artifactId>javax.servlet-api</artifactId>
		    <version>${servlet-version}</version>
		    <scope>provided</scope><!-- 編譯時有效 -->
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>${jsp-version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>jstl</groupId>
			<artifactId>jstl</artifactId>
			<version>${jstl-version}</version>
		</dependency>
		<!-- spring -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${spring-version}</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>${aspectjweaver-version}</version>
		</dependency>
		<!-- mybatis -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>${mybatis-version}</version>
		</dependency>
		<!-- mybatis和spring整合 -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>${mybatis-spring-version}</version>
		</dependency>
		<!-- log4j 日誌 -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>${log4j-version}</version>
		</dependency>
		<!-- mysql 驅動類 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>${mysql-connector-java-version}</version>
		</dependency>
		<!-- jackson -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>${jackson-version}</version>
		</dependency>
		<!-- apache 圖片上傳 -->
		<dependency> 
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>${commons-fileupload-version}</version>
		</dependency>
	</dependencies>
	<build>
		<resources>
			<resource>
				<directory>src/main/java</directory>
				<includes>
					<include>**/*.xml</include>
				</includes>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
				<includes>
					<include>**/*.xml</include>
					<include>**/*.properties</include>
				</includes>
			</resource>
		</resources>
		<plugins>
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<version>2.2</version>
				<configuration>
					<port>8080</port>
					<path>/</path>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

到這裡maven專案環境就好了,可以開始配置ssm

applicationContext.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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" 
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd" default-autowire="byName">
    <!-- 註解掃描 -->
    <context:component-scan base-package="cn.bl"></context:component-scan>
    <!-- 載入屬性檔案 -->
    <context:property-placeholder location="classpath:db.properties"/>
    <!-- 資料來源 -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    	<property name="driverClassName" value="${jdbc.driver}"></property>
    	<property name="url" value="${jdbc.url}"></property>
    	<property name="username" value="${jdbc.username}"></property>
    	<property name="password" value="${jdbc.password}"></property>
    </bean>
    <!-- SqlSessionFactory -->
    <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
    	<property name="dataSource" ref="dataSource"></property>
    	<property name="typeAliasesPackage" value="cn.bl.domain"></property>
    </bean>
    <!-- 掃描器 --> 
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    	<property name="basePackage" value="cn.bl.mapper"></property>
    	<property name="sqlSessionFactoryBeanName" value="factory"></property>
    </bean>
    <!-- 事務管理器 -->
    <bean id="txManage" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    	<property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 宣告式事務 -->
    <tx:advice id="txAdvice" transaction-manager="txManage">
    	<tx:attributes>
    		<tx:method name="ins*"/>
    		<tx:method name="del*"/>
    		<tx:method name="upd*"/>
    		<tx:method name="*" read-only="true"/>
    	</tx:attributes>
    </tx:advice>
    <!-- 配置aop -->
    <aop:config>
    	<aop:pointcut expression="execution(* cn.bl.service.*.*(..))" id="mypoint"/>
    	<aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint"/>
    </aop:config>
</beans>

db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/maven
jdbc.username = root
jdbc.password = 000

log4j.properties

log4j.rootCategory=ERROR, CONSOLE ,LOGFILE

log4j.logger.com.bjsxt.mapper=DEBUG

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%C %d{YYYY-MM-dd hh:mm:ss}  %m %n

log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=F:/my.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%m %n

springmvc.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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!-- 掃描註解 -->
    <context:component-scan base-package="cn.bl"></context:component-scan>
    <!-- 註解驅動 -->
    <mvc:annotation-driven></mvc:annotation-driven>
    <!-- 放行靜態資源 -->
    <mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
    <mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
    <mvc:resources location="/files/" mapping="/files/**"></mvc:resources>
</beans>

ok 建立資料庫

user.java

package cn.bl.domain;

public class User {
	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;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
	}
	
}

UserMapper.java

package cn.bl.mapper;

import cn.bl.domain.User;

public interface UserMapper {
	public abstract User login(User user); 
}

UserMapper.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.bl.mapper.UserMapper" >
	<select id="login" resultType="cn.bl.domain.User">
		select * from user where username=#{username}  and password=#{password} 
	</select>
</mapper>

UserDAO.java

public interface UserDAO {
	public abstract User login(User user); 
}

UserImpl.java

package cn.bl.dao;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Repository;

import cn.bl.domain.User;
import cn.bl.mapper.UserMapper;

@Repository(value="userDAO")
public class UserImpl implements UserDAO{
	@Resource(name="factory")
	private SqlSessionFactory factory;
	@Override
	public User login(User user) {
		SqlSession session = factory.openSession();
		//System.out.println("username:"+user.getUsername()+",password:"+user.getPassword());
		User res = session.getMapper(UserMapper.class).login(user);
		//System.out.println(res);
		return res;
	}
}
public interface IUserService {
	public abstract User login(User user); 
}
@Service(value="userService")
public class UserService implements IUserService{
	@Resource(name="userDAO")
	private UserDAO dao;
	@Override
	public User login(User user) {
		return dao.login(user);
	}
}

UserController

package cn.bl.controller;

import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.bl.domain.User;
import cn.bl.service.IUserService;

@Controller
public class UserController {
	
	@Resource(name="userService")
	private IUserService service;
	
	@RequestMapping("/login")
	public String login(User user,PrintWriter out,HttpSession session) {
		User res = service.login(user);
		System.out.println("con:"+res);
		if(res!=null) {
			session.setAttribute("curUser", res);//這個curUser不能寫成user,否則出錯:jsp中找到的是原來的user
		}else {
			session.invalidate();
		}
		return "index.jsp";
	}
}

 

專案右鍵,run as -- maven build --- goals填寫:clean tomcat7:run ,回車