zoukankan      html  css  js  c++  java
  • spring+ibatis环境搭建

    简单的spring+ibatis入门实例:ibatis是一种半自动化的持久层框架,它介于JDBC和hibernate之间,使用比较灵活。

    一:目录结构

    二:需要导入的jar包:

    所有的第三方jar包都需要加上,spring.jar、ibatis.-2.3.3.720.jar、sqlijdbc.jar、oscache-2.4.jar、commons-pool-1.3.jar、commons-dbcp-1.4.jar,mysql-connector-5.0.5.jar。不然运行的时候都要报错,这里需要注意SqlMapClientDaoSupport类需要的jar包来自于spring.jar,不能导错。

    三:代码

    1.bean包

    bean包封装了POJO对象User,有两个属性id和name

    如下:

     1 /**
     2  * 
     3  */
     4 package com.hlcui.bean;
     5 
     6 /**
     7  * @author Administrator
     8  * 
     9  */
    10 public class User {
    11     private int id;
    12     private String name;
    13 
    14     
    15 
    16     public int getId() {
    17         return id;
    18     }
    19 
    20     public void setId(int id) {
    21         this.id = id;
    22     }
    23 
    24     public String getName() {
    25         return name;
    26     }
    27 
    28     public void setName(String name) {
    29         this.name = name;
    30     }
    31 
    32 }

    2. Dao包

     Dao里面有一个PersonOperDao和PersonOperDaoImpl。PersonOperDao是一个接口,PersonOperDaoImpl实现了Dao的接口。

    PersonOperDao代码如下:

     1 /**
     2  * 
     3  */
     4 package com.hlcui.dao;
     5 
     6 import java.util.List;
     7 
     8 import com.hlcui.bean.User;
     9 
    10 /**
    11  * @author Administrator
    12  *
    13  */
    14 public interface PersonOperDao {
    15     public List<User> getAllUsers();
    16     public User getUserById(int id);
    17     public void save(User user);
    18     public void delete(int id);
    19     public void modify(User user);
    20 }

    PersonOperDaoImpl代码如下:

     1 /**
     2  * 
     3  */
     4 package com.hlcui.dao;
     5 
     6 import java.util.List;
     7 
     8 import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
     9 
    10 import com.hlcui.bean.User;
    11 
    12 /**
    13  * @author Administrator
    14  * 
    15  */
    16 public class PersonOperDaoImpl extends SqlMapClientDaoSupport implements
    17         PersonOperDao {
    18 
    19     @SuppressWarnings("unchecked")
    20     @Override
    21     public List<User> getAllUsers() {
    22         return this.getSqlMapClientTemplate().queryForList("getAllUsers", null);
    23     }
    24 
    25     @Override
    26     public User getUserById(int id) {
    27         return (User) this.getSqlMapClientTemplate().queryForObject("getUserById",
    28                 id);
    29     }
    30 
    31     @Override
    32     public void save(User user) {
    33         this.getSqlMapClientTemplate().insert("saveUser", user);
    34     }
    35 
    36     @Override
    37     public void delete(int id) {
    38         this.getSqlMapClientTemplate().delete("deleteUser", id);
    39     }
    40 
    41     @Override
    42     public void modify(User user) {
    43         this.getSqlMapClientTemplate().update("modifyUser", user);
    44     }
    45 
    46 }

    四:配置文件

    除此之外,关键的问题还有三个配置文件。applicationcontext.xml、ibatis.xml、sqlMapConfig.xml。

    applicationContext.xml文件是spring的配置文件,

    1:配置数据源 2:配置sqlmapclient配置文件  3:引入配置文件sqlmap

    如下:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
     5 
     6     <!-- 配置数据源 -->
     7     <bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
     8         <property name="driverClassName">
     9             <value>com.mysql.jdbc.Driver</value>
    10         </property>
    11         <property name="username">
    12             <value>root</value>
    13         </property>
    14         <property name="password">
    15             <value>root</value>
    16         </property>
    17         <property name="url">
    18             <value>jdbc:mysql://localhost:3306/test</value>
    19         </property>
    20     </bean>
    21 
    22     <!-- 引入sqlmap配置文件 -->
    23     <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
    24         <property name="configLocation">
    25             <value>sqlMapConfig.xml</value>
    26         </property>
    27     </bean>
    28 
    29     <!-- 配置DAO -->
    30     <bean id="PersonOperDaoImpl" class="com.hlcui.dao.PersonOperDaoImpl">
    31         <property name="dataSource">
    32             <ref bean="datasource" />
    33         </property>
    34         <property name="sqlMapClient">
    35             <ref bean="sqlMapClient" />
    36         </property>
    37     </bean>
    38 </beans>
     


    ibatis.xml内容如下:

     1 <?xml version="1.0" encoding="UTF-8" ?>  
     2 <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >  
     3 <sqlMap >  
     4   <typeAlias type="com.hlcui.bean.User" alias="user"/>  
     5   <resultMap id="ibatisTest" class="user" >  
     6     <result column="id" property="id" jdbcType="int" />  
     7     <result column="name" property="name" jdbcType="varchar" />  
     8   </resultMap>  
     9     
    10   <!-- 获得全查询列表 -->  
    11   <select id="getAllUsers" resultMap="ibatisTest">  
    12     select * from User  
    13   </select>  
    14     
    15   <!-- 根据id获得用户对象 -->  
    16     <select id="getUserById" resultMap="ibatisTest">  
    17      select * from User where id=#value#  
    18   </select>   
    19     
    20    <!-- 新增用户对象 -->  
    21    <insert id="saveUser" parameterClass="user">  
    22       insert into User (id,name) values (#id#,#name#)  
    23    </insert>  
    24      
    25    <!-- 删除用户对象 -->  
    26    <delete id="deleteUser">  
    27      delete from User where id=#value#  
    28    </delete>  
    29      
    30    <!-- 更新用户对象 -->  
    31    <delete id="modifyUser" parameterClass="user">  
    32       update User set name=#name# where id=#id#  
    33    </delete>  
    34 </sqlMap>  

    sqlMapConfig.xml文件如下:

    1 <?xml version="1.0" encoding="UTF-8"?>
    2 <!DOCTYPE sqlMapConfig   
    3 PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"   
    4 "http://www.ibatis.com/dtd/sql-map-config-2.dtd">
    5 <sqlMapConfig>
    6     <sqlMap resource="ibatis.xml" />
    7 </sqlMapConfig> 

    五:测试语句

     1 /**
     2  * 
     3  */
     4 package com.hlcui.test;
     5 
     6 import java.util.List;
     7 
     8 import org.junit.Test;
     9 import org.springframework.context.ApplicationContext;
    10 import org.springframework.context.support.ClassPathXmlApplicationContext;
    11 
    12 import com.hlcui.bean.User;
    13 import com.hlcui.dao.PersonOperDao;
    14 
    15 /**
    16  * @author Administrator
    17  * 
    18  */
    19 public class TestUserOper {
    20     /**
    21      * 测试获取所有用户信息
    22      */
    23     @Test
    24     public void testFindAllUsers() {
    25         List<User> users = getDAO().getAllUsers();
    26         for (User u : users) {
    27             System.out.println("编号:" + u.getId() + "  姓名:" + u.getName());
    28         }
    29     }
    30 
    31     /**
    32      * 测试根据id获取对象信息
    33      */
    34     @Test
    35     public void testFindUser() {
    36         int id = 1;
    37         User user = getDAO().getUserById(id);
    38         System.out.println("编号:" + id + "  姓名:" + user.getName());
    39     }
    40 
    41     /**
    42      * 测试插入用户信息
    43      */
    44     @Test
    45     public void testSaveUserInfo() {
    46         User u = new User();
    47         u.setId(2);
    48         u.setName("Lucy");
    49         getDAO().save(u);
    50     }
    51 
    52     /**
    53      * 测试删除用户信息
    54      */
    55     @Test
    56     public void testDeleteUserInfo() {
    57         int id = 2;
    58         getDAO().delete(id);
    59     }
    60 
    61     /**
    62      * 测试修改用户信息
    63      */
    64     @Test
    65     public void testModifyUserInfo() {
    66         int id = 1;
    67         User user = getDAO().getUserById(id);
    68         user.setName("Lili");
    69         getDAO().modify(user);
    70     }
    71 
    72     /**
    73      * 获取DAO对象
    74      * 
    75      * @return
    76      */
    77     public PersonOperDao getDAO() {
    78         ApplicationContext ac = new ClassPathXmlApplicationContext(
    79                 "applicationContext.xml");
    80         return (PersonOperDao) ac.getBean("PersonOperDaoImpl");
    81     }
    82 }

    以上代码均已验证。

  • 相关阅读:
    Android 关于调用系统内已安装的相机问题
    bae中的wordpress:正在执行例行维护,请一分钟后回来,时间过长任然不行的解决办法
    CefSharp 拦截WebSocket发送和接收数据 WS
    CefSharp发送POST请求、GET请求
    CefSharp 拦截Request、Response获取文件内容js、css、图片、CefSharp拦截ajax内容等
    CefSharp动态设置代理IP、设置UserAgent等 UA Proxy
    CefSharp QQ群提取
    CefSharp 免登陆
    CefSharp过天猫、淘宝验证码、滑动验证码、cefsharp高版本
    Cefsharp实现拼多多 后台 多开,免登录
  • 原文地址:https://www.cnblogs.com/warrior4236/p/5625319.html
Copyright © 2011-2022 走看看