搭建struts2+spring+ibatis框架整合需要考虑以下几个步骤:
- Maven配置和相关依赖
- 数据库配置和数据源配置
- 配置Spring与Mybatis整合
- 配置Spring与Struts2框架整合
下面将逐步为您演示搭建struts2+spring+ibatis框架整合的完整攻略,并提供2条示例。
1. Maven配置和相关依赖
在pom.xml文件中,添加以下配置依赖:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.opensymphony.xwork2</groupId>
<artifactId>xwork-core</artifactId>
<version>2.3.24.3</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.24.3</version>
</dependency>
2. 数据库配置和数据源配置
在数据库中创建一个表,并在配置文件中添加以下内容:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.example.model"/>
<property name="mapperLocations" value="classpath*:com/example/mapper/*.xml"/>
</bean>
3. 配置Spring与Mybatis整合
在Spring配置文件中加入以下内容:
<bean id="bookDao" class="com.example.dao.IBookDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<bean id="bookService" class="com.example.service.BookServiceImpl">
<property name="bookDao" ref="bookDao"/>
</bean>
创建Book实体类
public class Book {
private int id;
private String name;
private String author;
private float price;
// 省略getter和setter
}
创建BookMapper映射器接口
public interface BookMapper {
public void addBook(Book book);
public void deleteBook(int id);
public void updateBook(Book book);
public Book getBookById(int id);
}
创建BookMapper映射器接口的实现类
public class BookDaoImpl implements BookMapper {
private SqlSessionFactory sqlSessionFactory;
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
this.sqlSessionFactory = sqlSessionFactory;
}
@Override
public void addBook(Book book) {
SqlSession sqlSession = sqlSessionFactory.openSession();
sqlSession.insert("addBook", book);
sqlSession.commit();
sqlSession.close();
}
// 省略其他方法实现
}
4. 配置Spring与Struts2框架整合
创建Struts2的Action类:
public class BookAction extends ActionSupport {
private Book book;
private IBookService bookService;
// getter、setter省略
@Override
public String execute() throws Exception {
bookService.addBook(book);
return SUCCESS;
}
}
在Struts2的配置文件中加入以下内容:
<bean id="bookAction" class="com.example.action.BookAction">
<property name="bookService" ref="bookService"/>
</bean>
<package name="default" namespace="/" extends="struts-default">
<action name="add" class="bookAction">
<result name="success">/result.jsp</result>
</action>
</package>
现在,我们完成了struts2+spring+ibatis框架整合的配置。下面提供2条示例操作:
- 添加图书
在浏览器地址栏中输入添加图书的URL,如下:
http://localhost:8080/book/add?name=《Java编程思想》&author=Bruce.Eckel&price=50
- 删除图书
在浏览器地址栏中输入删除图书的URL,如下:
http://localhost:8080/book/delete?id=1
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:struts2+spring+ibatis框架整合实现增删改查 - Python技术站