Spring-Struts-Mybatis整合

此爲作者原創  轉載請註明出處

在這裏我使用的是struts2.3.7+Spring4.3.4+Mybatis3.4.1

首先需要導入jar包(需要注意包衝突如log4j重複javassist-3.18.1-GA.jar重複等)

然後配置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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Spring_Struts_MyBatis</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<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>*.action</url-pattern>
</filter-mapping>
</web-app>

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: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.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">

<!-- 引入配置文件 -->
<context:property-placeholder location="classpath:db.properties" />

<!-- 數據源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>

<!-- 創建Mybatis SessionFactory -->
<bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 給session工廠配置數據源 -->
<property name="dataSource" ref="dataSource" />
<!-- 告訴session工廠mybatis的核心配置在哪裏 -->
<property name="configLocation" value="classpath:mybatis.xml"/>
</bean>

<!-- 事務管理者 -->
<bean id="txMananger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>

<!-- 開啓事務 -->
<tx:annotation-driven transaction-manager="txMananger"/>

<!--創建數據映射器,數據映射器必須爲接口 -->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.lcl.dao.IMybatisDao" />
<property name="sqlSessionFactory" ref="SqlSessionFactory" />
</bean>

<!-- service -->
<bean id="myService" class="com.lcl.service.impl.MyService">
<!-- 這個Dao就是註冊在Mybatis中的Mapper -->
<property name="mybatisDao" ref="userMapper"/>
</bean>

<!-- strutsAction -->
<bean id="struts_action" class="com.lcl.action.StrutsAction" >
<property name="service" ref="myService"></property>
</bean>

</beans>

Mybatis核心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>
<mappers>
<mapper class="com.lcl.dao.IMybatisDao"/>
</mappers>
</configuration>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<package name="spring_struts" namespace="/" extends="struts-default">
<action name="struts" class="struts_action">
<result type="redirect">/struts.jsp</result>
</action>
</package>
</struts>

Service接口
package com.lcl.service;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.lcl.entity.UserBean;
@Transactional
public interface IService {

public void add(String username, int password);
public void remove(int password);
public void modify(String username, int password, int id);
public UserBean findById(int id);
public List<UserBean> findAll();

}

package com.lcl.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lcl.dao.IMybatisDao;
import com.lcl.entity.UserBean;
import com.lcl.service.IService;

@Service
public class MyService implements IService {

@Autowired
private IMybatisDao mybatisDao;

public IMybatisDao getMybatisDao() {
return mybatisDao;
}

public void setMybatisDao(IMybatisDao mybatisDao) {
this.mybatisDao = mybatisDao;
}

public void add(String username, int password) {
mybatisDao.insert(username, password);
}

public void remove(int password) {
mybatisDao.deleteById(password);
}

public void modify(String username, int password, int id) {
mybatisDao.update(username, password, id);
}

public UserBean findById(int id) {
return mybatisDao.selectById(id);
}

public List<UserBean> findAll() {
return mybatisDao.selectAll();
}

}

實體類
package com.lcl.entity;

public class UserBean {

private String username;
private int password;
private int id;

public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getPassword() {
return password;
}
public void setPassword(int password) {
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}

}

mybatis數據庫查詢語句
package com.lcl.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.lcl.entity.UserBean;

public interface IMybatisDao {

@Insert("insert into user(username,password) values(#{username},#{password})")
public void insert(@Param("username")String username,@Param("password")int password);

@Delete("delete from user where id=#{id}")
public void deleteById(@Param("id")int id);

@Update("update user set username=#{username},password=#{password} where id=#{id}")
public void update(@Param("username")String username,@Param("password")int password,@Param("id")int id);

@Select("select * from user where id=#{id}")
public UserBean selectById(int id);

@Select("select * from user")
public List<UserBean> selectAll();

}

Struts的action
package com.lcl.action;

import java.util.List;

import com.lcl.entity.UserBean;
import com.lcl.service.IService;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class StrutsAction extends ActionSupport {

private String username;
private String password;
private IService service;

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;
}

public IService getService() {
return service;
}

public void setService(IService service) {
this.service = service;
}

@Override
public String execute() throws Exception {
service.add("mybatis", 123);
List<UserBean> findAll = service.findAll();
System.out.println(findAll);
return SUCCESS;
}

}

目錄結構

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章