• Mybatis基于SqlSession实现CRUD


    之前我们讲的基于XML还是接口注解配置都是使用接口实现CRUD,本文我们将要讲解通过splsession来实现CRUD,这种方法比较灵活。

    基本配置

     1  <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
     2     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
     3         <property name="dataSource" ref="dataSource" />
     4         <property name="mapperLocations" value="classpath:mapping/*.xml"></property>
     5         <!-- 分页插件 -->
     6         <property name="plugins">
     7             <array>
     8                 <bean class="com.github.pagehelper.PageHelper">
     9                     <property name="properties">
    10                         <value>
    11                             dialect=mysql
    12                         </value>
    13                     </property>
    14                 </bean>
    15             </array>
    16         </property>
    17     </bean>
    18 
    19 
    20     <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    21         <constructor-arg index="0" ref="sqlSessionFactory" />
    22     </bean>
    View Code

    Mapper实现

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
     3 <mapper namespace="sysUserMapper">
     4     <select id="SelectOne" parameterType="java.util.HashMap" resultType="java.util.HashMap">
     5     select * from user_ where id = #{id,jdbcType=VARCHAR} limit 0,20
     6   </select>
     7     <select id="SelectProc" parameterType="java.util.HashMap" resultType="java.util.HashMap" >
     8         {call get_user(#{id,jdbcType=VARCHAR})}
     9   </select>
    10     <select id="SelectList" parameterType="java.util.HashMap" resultType="java.util.HashMap" >
    11         select * from user_
    12         <where>
    13             <if test="male_ != null and male_!=''">
    14                 AND  male_ = #{male_,jdbcType=VARCHAR}
    15             </if>
    16         </where>
    17         limit 0,20
    18     </select>
    19     <insert id="insert" parameterType="java.util.HashMap">
    20         insert into user_test(id, name) values(#{id}, #{name})
    21     </insert>
    22 </mapper>
    View Code

    数据实现

    首先我们 添加SqlSession 属性注入

    1 @Autowired
    2     private  SqlSession sqlSession;
    View Code

    然后我们实现具体的CRUD操作

     1 public Map<String, String> SelectOne(String method,Object Para)
     2     {
     3         return sqlSession.selectOne(method, Para);
     4     }
     5 
     6     public List<Map<String, String>> SelectList(String method,Object Para)
     7     {
     8         return sqlSession.selectList(method, Para);
     9     }
    10 
    11     public List<Map<String, String>> SelectProc(String method,Object Para)
    12     {
    13         return sqlSession.selectList(method, Para);
    14     }
    15 
    16 
    17     public void  insert(String method,Object Para)
    18     {
    19         if(Para.getClass().getName().equals("java.util.ArrayList"))
    20         {
    21             ArrayList list = (ArrayList) Para;
    22             for (int i = 0; i < list.size(); i++) {
    23                 HashMap hm = (HashMap) list.get(i);
    24                 sqlSession.insert(method, hm);
    25             }
    26         }
    27         else
    28         {
    29              sqlSession.insert(method, Para);
    30         }
    31     }
    32 
    33 
    34     public void  update(String method,Object Para)
    35     {
    36         if(Para.getClass().getName().equals("java.util.ArrayList"))
    37         {
    38             ArrayList list = (ArrayList) Para;
    39             for (int i = 0; i < list.size(); i++) {
    40                 HashMap hm = (HashMap) list.get(i);
    41                 sqlSession.update(method, hm);
    42             }
    43         }
    44         else
    45         {
    46             sqlSession.update(method, Para);
    47         }
    48 
    49     }
    50 
    51 
    52     public void delete(String method,Object Para)
    53     {
    54         if(Para.getClass().getName().equals("java.util.ArrayList"))
    55         {
    56             ArrayList list = (ArrayList) Para;
    57             for (int i = 0; i < list.size(); i++) {
    58                 HashMap hm = (HashMap) list.get(i);
    59                 sqlSession.delete(method, hm);
    60             }
    61         }
    62         else
    63         {
    64             sqlSession.delete(method, Para);
    65         }
    66     }
    View Code

    路由中转操作

     1 public  Object doProess(String BsCode,String Operation, Object Para) {
     2         String method=BsCode.trim()+"."+Operation.trim();
     3        // try {
     4             switch (Operation) {
     5                 case "SelectOne":
     6                     return SelectOne(method, Para);
     7                 case "SelectList":
     8                     return SelectList(method, Para);
     9                 case "SelectProc":
    10                     return SelectProc(method, Para);
    11                 case "insert":
    12                      insert(method, Para);
    13                      return 1;
    14                 case "update":
    15                      update(method, Para);
    16                      return 1;
    17                 case "delete":
    18                      delete(method, Para);
    19                      return 1;
    20                 default:
    21                     return "无相关操作类型";
    22 
    23             }
    24        // }catch (Exception e)
    25        // {
    26            // return e.getMessage();
    27        // }
    28     }
    View Code

    数据验证测试

     1 @RunWith(SpringJUnit4ClassRunner.class)  //使用junit4进行测试
     2 @ContextConfiguration({"classpath:spring-base.xml"})
     3 public class commServiceImplTest {
     4 
     5     @Autowired
     6     private commService commservice;
     7 
     8     @Test
     9     public void selectOne() throws Exception {
    10         String bsCode="sysUserMapper";
    11         String Operation="SelectOne";
    12         Map<String, String> para=new HashMap<String, String>();
    13         para.put("id","fjx");
    14         Map<String, String> userinfo=(Map<String, String>) commservice.doProess(bsCode,Operation, para);
    15         System.out.print(userinfo);
    16     }
    17 
    18    @Test
    19     public void doProess2() throws Exception {
    20         String bsCode="sysUserMapper";
    21         String Operation="SelectProc";
    22         Map<String, String> para=new HashMap<String, String>();
    23         para.put("id","fjx");
    24         Object userinfo=commservice.doProess(bsCode,Operation,para);
    25         System.out.print(userinfo);
    26     }
    27 
    28     @Test
    29     public void doProess3() throws Exception {
    30         String bsCode="sysUserMapper";
    31         String Operation="SelectList";
    32         Map<String, String> para=new HashMap<String, String>();
    33         para.put("male_","1");
    34         Object users=commservice.doProess(bsCode,Operation,para);
    35         System.out.print(users);
    36     }
    37 
    38 }
    View Code

    GITHUB

    github :  https://github.com/nbfujx/Goku.WebService.Bus

  • 相关阅读:
    网化商城
    ITU R-REC-S 系列建议书分类
    UDLua
    libev简单使用
    Windows 审计日志 安全部分不刷新的解决办法
    sys.version_info
    mitmproxy 安装
    Python3 os.remove() 方法
    Python3 os.rename() 方法
    python3 unittest
  • 原文地址:https://www.cnblogs.com/nbfujx/p/7768415.html
Copyright © 2020-2023  润新知