mybatis的mapping基本相关操作
来源:互联网 发布:西班牙 旅游 知乎 编辑:程序博客网 时间:2024/06/05 05:22
本文章的demo用map代替了javabean,其实javabean的属性和值就相当于map的key-value,所以就不那么麻烦用javabean了。闲话少说,程序员就是要的干脆利落,上代码再说:
1、测试类
/** * Created by wolf on 15/11/12. */public class UserTestDemoService extends MybatisSupportBaseService { public static int saveAndUpdate(Map map) { SqlSessionManager manager = getSqlSessionManager(); SqlSession session = manager.openSession(false); try { session.insert("test.saveUser", map); session.update("test.updateUser", map); session.commit(); } catch (Exception e) { session.rollback(); e.printStackTrace(); }finally { session.close(); } return 1; } public static Map queryUser(Map map) { return selectOne("test.queryUser", map); } public static List selectUser(Map map) { return selectList("test.selectUser", map); } public static List whereUser(Map map) { return selectList("test.whereUser", map); } public static int setUpdateUser(Map map) { return update("test.setUpdateUser", map); } public static List selectIn(Map map) { return selectList("test.selectIn", map); } public static void main(String[] args) { Map m = new HashMap(); /*m.put("id", "wolf001"); m.put("user_name", "wolf_bin1111"); m.put("user_id", "wolf_id"); m.put("user_age", "22");*/ String[] arr = new String[]{"002","001"}; m.put("list", arr);// System.out.println(saveAndUpdate(m));// System.out.println(queryUser(m));// System.out.println(selectUser(m));// System.out.println(whereUser(m));// System.out.println(setUpdateUser(m)); System.out.println(selectIn(m)); }}
2、mapping文件test.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="test"> <sql id="userAtr"> #{id},#{user_id},#{user_name},#{user_age} </sql> <sql id="userPro"> id,user_id,user_name,user_age </sql> <insert id="saveUser" parameterType="java.util.HashMap"> insert into t_test_user (<include refid="userPro"></include>) values (<include refid="userAtr"></include>) </insert> <update id="updateUser" parameterType="java.util.HashMap"> UPDATE TABLE t_test_user SET user_name=#{user_name} WHERE id=#{id} </update> <select id="queryUser" parameterType="java.util.HashMap" resultType="java.util.HashMap"> SELECT <include refid="userPro"></include> FROM t_test_user WHERE 1=1 <if test="user_name != null"> AND user_name LIKE '%${user_name}%' </if> </select> <select id="selectUser" parameterType="java.util.HashMap" resultType="java.util.HashMap"> SELECT <include refid="userPro"></include> FROM t_test_user WHERE 1=1 <choose> <when test="id!=null"> AND id=#{id} </when> <when test="user_name != null"> AND user_name LIKE '%${user_name}%' </when> <otherwise> </otherwise> </choose> </select> <select id="whereUser" parameterType="java.util.HashMap" resultType="java.util.HashMap"> SELECT <include refid="userPro"></include> FROM t_test_user <where> <if test="user_name != null"> user_name LIKE '%${user_name}%' </if> <if test="user_age != null"> AND user_age = #{user_age} </if> </where> </select> <select id="selectIn" resultType="java.util.HashMap"> SELECT * FROM t_test_user WHERE ID in <foreach item="item" index="index" collection="list" open="(" separator="," close=")"> #{item} </foreach> </select> <update id="setUpdateUser" > UPDATE t_test_user <trim prefix="set" suffixOverrides=","> <if test="user_name != null">user_name=#{user_name},</if> <if test="user_age != null">user_age=#{user_age},</if> </trim> WHERE user_id = #{user_id} </update></mapper>
3、读取mybatis配置类
public class MybatisSupportBaseService{ private static SqlSessionManager manager; //初始化sqlSessionFactory sqlsessionFactory只有一个实例 static { String resource = "SqlMapConfig.xml"; Reader reader = null; try { reader = Resources.getResourceAsReader(resource); } catch (IOException e) { logger.error("加载mybatis配置异常", e); } manager = SqlSessionManager.newInstance(reader); } /** * 通过获取SQLSessionFactory openSqlSession实例 自由控制事物 * @return */ public static SqlSessionManager getSqlSessionManager(){ return manager; } }4、mybatis配置SqlMapConfig.xml
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><settings><setting name="jdbcTypeForNull" value="NULL" /></settings><plugins><!-- com.github.pagehelper为PageHelper类所在包名 --><plugin interceptor="com.github.pagehelper.PageHelper"><!--<property name="dialect" value="mysql"/>--><!-- 该参数默认为false --><!-- 设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用 --><!-- 和startPage中的pageNum效果一样--><property name="offsetAsPageNum" value="true"/><!-- 该参数默认为false --><!-- 设置为true时,使用RowBounds分页会进行count查询 --><property name="rowBoundsWithCount" value="true"/><!-- 设置为true时,如果pageSize=0或者RowBounds.limit = 0就会查询出全部的结果 --><!-- (相当于没有执行分页查询,但是返回结果仍然是Page类型) <property name="pageSizeZero" value="true"/>--><!-- 3.3.0版本可用 - 分页参数合理化,默认false禁用 --><!-- 启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页 --><!-- 禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据 --><property name="reasonable" value="true"/><!-- 3.5.0版本可用 - 为了支持startPage(Object params)方法 --><!-- 增加了一个`params`参数来配置参数映射,用于从Map或ServletRequest中取值 --><!-- 可以配置pageNum,pageSize,count,pageSizeZero,reasonable,不配置映射的用默认值 --><!-- 不理解该含义的前提下,不要随便复制该配置 <property name="params" value="pageNum=start;pageSize=limit;"/> --></plugin></plugins><!-- 配置Mybatis的环境,事务及数据源等等 --><environments default="development"><environment id="development"><transactionManager type="JDBC" /><dataSource type="DataSourceAdapterFactory"></dataSource></environment></environments><!-- 指定映射文件或者映射类 --><mappers><mapper resource="test.xml"/></mappers></configuration>5、读取资源数据库资源类
public class DataSourceAdapterFactory implements DataSourceFactory { public DataSourceAdapterFactory() { } public DataSource getDataSource() { try { Properties e = PropertiesUtil.loadPropertyFile("druidconfig.properties"); return DruidDataSourceFactory.createDataSource(e); } catch (Exception var2) { var2.printStackTrace(); return null; } } public void setProperties(Properties properties) { }}
6、数据库资源文件druidconfig.properties
url:jdbc:oracle:thin:@127.0.0.1:1521:testdriverClassName:oracle.jdbc.driver.OracleDriverusername:rootpassword:rootinitialSize:1maxActive:20minIdle:10maxWait:60000useUnfairLock=true基本上就完成了。
1 0
- mybatis的mapping基本相关操作
- [mybatis]基本的CRUD操作
- MyBatis的增删改查基本操作
- Mybatis 增删改查的基本操作
- mybatis笔记-1-数据库的基本操作
- Mybatis-02-对数据库的基本操作
- mybatis基本操作
- mybatis基本操作
- 二.Mybatis基本操作
- MyBatis基本操作
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- OpenDPI-1.3.0源代码分析
- InputStream的三个read的区别
- HOSt ip is not allowed to connect to this MySql server
- android studio 手动更新
- functioncharts与extjs结合,做多折线图
- mybatis的mapping基本相关操作
- 8.4 AlarmManager实现精准定时任务
- 手工实现ARP中间人攻击
- 值动画
- make: 警告:检测到时钟错误。您的创建可能是不完整的。
- 程序员生存定律--成长路上常见的坑
- 单片机C语言代码-代码格式
- 【C++】多态的实现原理
- com库内存管理