1. 程式人生 > >Struts2+spring+mybatis註解模式

Struts2+spring+mybatis註解模式

匯入相應的jar包

先寫好db.properties檔案,用於存放資料庫的相關資訊

driver=com.mysql.jdbc.Driver
password=root
username=root
url=jdbc\:mysql\://localhost\:3306/he

新建spring的配置檔案
<?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-3.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
       
        
  	<bean id="propertyConfigurer"  
    	class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
	    <property name="location" value="classpath:config/db.properties"/>   
    	<property name="fileEncoding" value="utf-8" />  
	</bean>


   <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="${driver}"/>
			<property name="url" value="${url}"/>
			<property name="username" value="${username}"/>
			<property name="password" value="${password}"/>
   </bean>
    
    <context:annotation-config/>
    
    <context:component-scan base-package="com.mybatis"/>
    
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:com/mybatis/mapper/*"/> 
    </bean>
    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
     	<property name="basePackage" value="com.mybatis.dao"/>
    </bean>
    
</beans>

在web.xml中填寫相應的資訊
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	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_2_5.xsd">
  
  <context-param>
  	 <param-name>contextConfigLocation</param-name>
   	<param-value>classpath:config/spring-mybatis.xml</param-value>
  </context-param>
  
    <!-- spring監聽器 -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <filter>
     <filter-name>struts2</filter-name>
     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <listener>
    <display-name>時間的監聽器</display-name>
    <listener-class>com.mybatis.utils.Timers</listener-class>
  </listener>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

在src中新建mapper的配置檔案

 <?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="com.mybatis.dao.UserMapper">
        
         <select id="getUser" resultMap="userMap">
             select*from xiu
         </select>
        
         <select id="login" parameterType="com.mybatis.entity.User" resultType="java.lang.Integer"> 
           select count(*) from xiu where username=#{username} and password=#{password}
         </select>
 </mapper>
 
新建的resultmap
<?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="com.mybatis.dao.UserMapper">
  
    <resultMap type="com.mybatis.entity.User" id="userMap">
          <result property="id" column="id"/>
          <result property="username"  column="username"/>
          <result property="password" column="password"/>
    </resultMap>      
       
 </mapper>
 
利用mybatis的介面程式設計
package com.mybatis.dao;


import java.util.List;


import com.mybatis.entity.User;


/**
 * 
 * @author 戴禮明
 *日期: 2015-12-9
 *content:使用者介面
 */
public interface UserMapper {


<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> *使用者登入
<span style="white-space:pre">	</span> * @param user 需要登入的使用者
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>public int login(User user);
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * 查詢所有的使用者
<span style="white-space:pre">	</span> * @return 返回查詢結果
<span style="white-space:pre">	</span> */
    public  List<User> getUser();<span style="white-space:pre">	</span>
}