一.首先奉上項目目錄結構:java
說明: dao,mapping,model包下的全部內容能夠使用Generator工具自助生成。web
具體用法,能夠網上學習一下,比較簡單,主要作如下工做:spring
1.提供相關的數據庫的jar包及數據庫鏈接配置sql
2.提供實體類與表映射關係數據庫
3.提供一個生成代碼的空目錄。json
4.執行一條執行語句spring-mvc
進入到generator的目錄下,shift右鍵選擇 在此處打開命令窗口。session
執行 java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwritemybatis
二.核心配置文件spring.xml, spring-mybatis.xml,spring-mvc.xml 具體源碼mvc
1.spring.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" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<!-- 引入屬性文件 -->
<context:property-placeholder location="classpath:config.properties" />
<!-- 自動掃描(自動注入) -->
<context:component-scan base-package="com.joey.service" />
</beans>
2.spring-mybatis.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
">
<!-- JNDI方式配置數據源 -->
<!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="${jndiName}"></property> </bean> -->
<!-- 配置數據源 -->
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_username}" />
<property name="password" value="${jdbc_password}" />
<!-- 初始化鏈接大小 -->
<property name="initialSize" value="10" />
<!-- 鏈接池最大使用鏈接數量 -->
<property name="maxActive" value="20" />
<!-- 鏈接池最大空閒 -->
<property name="maxIdle" value="20" />
<!-- 鏈接池最小空閒 -->
<property name="minIdle" value="0" />
<!-- 獲取鏈接最大等待時間 -->
<property name="maxWait" value="60000" />
<!-- <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> -->
<property name="validationQuery" value="${validationQuery}" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="testWhileIdle" value="true" />
<!-- 配置間隔多久才進行一次檢測,檢測須要關閉的空閒鏈接,單位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一個鏈接在池中最小生存的時間,單位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="25200000" />
<!-- 打開removeAbandoned功能 -->
<property name="removeAbandoned" value="true" />
<!-- 1800秒,也就是30分鐘 -->
<property name="removeAbandonedTimeout" value="1800" />
<!-- 關閉abanded鏈接時輸出錯誤日誌 -->
<property name="logAbandoned" value="true" />
<!-- 監控數據庫 -->
<!-- <property name="filters" value="stat" /> -->
<property name="filters" value="mergeStat" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自動掃描entity目錄, 省掉Configuration.xml裏的手工配置 -->
<property name="mapperLocations" value="classpath:com/joey/mapping/*.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.joey.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
</beans>
3.spring-mvc.xml源碼
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" 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" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.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">
<!-- 自動掃描controller包下的全部類,使其認爲spring mvc的控制器 -->
<context:component-scan base-package="com.joey.controller" />
<!-- annotation driven -->
<mvc:annotation-driven />
<!-- 避免IE執行AJAX時,返回JSON出現下載文件 -->
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
<bean id="stringConverter"
class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 啓動Spring MVC的註解功能,完成請求和註解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonConverter" /><!-- json轉換器 -->
<ref bean="stringConverter" />
</list>
</property>
</bean>
<!-- 對模型視圖名稱的解析,即在模型視圖名稱添加先後綴 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp" />
<mvc:interceptors>
<bean class="com.joey.interceptor.TokenInterceptor" /> //全局攔截器
</mvc:interceptors>
</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" 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>JoeySSM</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml,classpath:spring-mybatis.xml</param-value>
</context-param>
<filter>
<description>字符集過濾器</description>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<description>字符集編碼</description>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<description>spring監聽器</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 防止spring內存溢出監聽器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- spring mvc servlet -->
<servlet>
<description>spring mvc servlet</description>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<description>spring mvc 配置文件</description>
<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>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/login.jsp</welcome-file>
</welcome-file-list>
<!-- 配置session超時時間,單位分鐘 -->
<session-config>
<session-timeout>20</session-timeout>
</session-config>
</web-app>
四. login.jsp頁面核心代碼截圖
五. 核心控制器部分代碼:
@Controller
@RequestMapping("userController")
public class UserController {
private static final Logger logger = Logger.getLogger(UserController.class);
@Autowired
public UrUserServiceI uruserService;
@RequestMapping("/login")
public String handleLogin(String username,String password,HttpServletRequest request){
logger.info(username+","+password);
String adminUser=ConfigUtils.getValue("admin");
String adminPwd=ConfigUtils.getValue("pwd");
if(username==null|password==null|username==""|password==""){
request.setAttribute("loginError", "用戶名密碼不能爲空");
return "login";
}else if(adminUser.equals(username.trim())&&adminPwd.equals(password.trim())){
request.getSession().setAttribute("user", username);
return "redirect:/userController/showUsers.do";
}else{
request.setAttribute("loginError", "用戶名密碼有誤");
return "login";
}
}
}