IDEA中集成struts2+mybatis

上一篇文章介绍了在IDEA中使用struts搭建一个简单的web项目,这篇文章主要引入mybatis,实现对db的操作,仅供参考。

为什么是Mybatis

选择mybatis主要有两个原因:
1. 之前任职过的一家比较大的互联网公司使用的就是mybatis,所以从稳定性和流行程度的角度来说肯定没问题;
2. 入手比较简单,一小时即可;

版本&jar包

struts2:2.3.31
(见http://blog.csdn.net/hfut_wowo/article/details/62887855)
Mybatis:3.4.2
JDBC:5.1.39

开始

  • 将jar包导入项目的WEB-INF/lib中,新建到mybatis library(具体见http://blog.csdn.net/hfut_wowo/article/details/62887855);
  • 创建mybatis配置文件:


<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8"/>
                <property name="username" value="root"/>
                <property name="password" value=""/>
            dataSource>
        environment>
    environments>
    <mappers>
        <mapper resource="mappers/CustomerInfoMapper.xml"/>
    mappers>
configuration>
  • 创建Mapper文,mappers/CustomerInfoMapper.xml:


<mapper namespace="CustomerInfoMapper">
    <select id="selectCustomerByBankID" parameterType="map" resultType="test.DO.CustomerInfoDO">
        select * from customer_info where `bank_id` = #{bank_id} limit 1
    select>
mapper>
  • 创建操作工具类,DBOperateUtil:
package test.util;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class DBOperateUtil {
    private SqlSessionFactory ssf;
    private static final DBOperateUtil dbou = new DBOperateUtil();

    public DBOperateUtil() {
        try {
            String resource = "MybatisConfig.xml";
            InputStream inputStream;
            inputStream = Resources.getResourceAsStream(resource);
            ssf = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static DBOperateUtil getInstance() {
        return dbou;
    }

    public SqlSessionFactory getSsf() {
        return ssf;
    }

    public void setSsf(SqlSessionFactory ssf) {
        this.ssf = ssf;
    }
}
  • 创建DO文件:
package test.DO;

public class CustomerInfoDO {
    private String bank_id;
    private int id;

    public String getBank_id() {
        return bank_id;
    }

    public void setBank_id(String bank_id) {
        this.bank_id = bank_id;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}
  • 在action中操作DB:
package test.action;

import org.apache.ibatis.session.SqlSession;
import test.DO.CustomerInfoDO;
import test.util.DBOperateUtil;

public class TestAction {
    private CustomerInfoDO customerInfoDO;

    public String execute() {
        SqlSession session = DBOperateUtil.getInstance().getSsf().openSession();
        CustomerInfoDO customerInfoDO = session.selectOne("CustomerInfoMapper.selectCustomerByBankID", "中国工商银行");
        this.setCustomerInfoDO(customerInfoDO);
        return "success";
    }

    public CustomerInfoDO getCustomerInfoDO() {
        return customerInfoDO;
    }

    public void setCustomerInfoDO(CustomerInfoDO customerInfoDO) {
        this.customerInfoDO = customerInfoDO;
    }
}
  • 在视图文件中显示从DB查出来的数据:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
  <head>
    <title>$Title$title>
  head>
  <body>
  <s:property value="customerInfoDO.bank_id"/>
  ${customerInfoDO.bank_id}
  body>
html>

上面的视图中采用了两种方式输出内容,最终的结果页面如下:
这里写图片描述

注:Mysql中数据表:

customer_info | CREATE TABLE `customer_info` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `bank_id` varchar(36) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

你可能感兴趣的:(Java,web)