码迷,mamicode.com
首页 > 数据库 > 详细

mybatis 数据库语句

时间:2020-10-08 19:19:06      阅读:30      评论:0      收藏:0      [点我收藏+]

标签:resource   inpu   version   XML   values   class   row   rgb   resources   

mybatis 操作数据库增删改查

package com.school.mybatis.junit;

import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import com.school.mybatis.pojo.sysUser;

public class mybatisTest {
    @Test
    public void testmybatis() throws Exception{
        //加载核心配置文件
        String resource = "sqlMapConfig.xml";
        InputStream in = Resources.getResourceAsStream(resource);
        //创建SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSessino = sqlSessionFactory.openSession();
        //执行sql语句
        //通过ID查询
        sysUser user1 = sqlSessino.selectOne("test.findUserById", 1);
        System.out.println(user1);
        //模糊查询
        List<sysUser> user2 = sqlSessino.selectList("test.findUserByUsername", "l");
        for (sysUser user3 : user2) {
            System.out.println(user3);
        }
        //添加用户
        sysUser suser = new sysUser();
        suser.setSysUserName("lhj");
        suser.setPassWord("123456");
        sqlSessino.insert("test.insersysUser",suser);
        sqlSessino.commit();
        
    }
    @Test
    public void testUpdateById() throws Exception{
        //加载核心配置文件
        String resource = "sqlMapConfig.xml";
        InputStream in = Resources.getResourceAsStream(resource);
        //创建SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSessino = sqlSessionFactory.openSession();
        //修改用户
        sysUser sysuser = new sysUser();
        sysuser.setId(1);
        sysuser.setSysUserName("lhj29");
        sysuser.setPassWord("123456");
        sqlSessino.update("test.UpdateUserById", sysuser);
        sqlSessino.commit();
        //删除用户
        sqlSessino.delete("test.deleteUserById", 1);
        sqlSessino.commit();
    }
    
}

操作数据库

<?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">
<!-- 写sql语句 -->
<!-- namespace为命名空间 -->
<mapper namespace="test">
    <!-- 通过ID查询一个用户  -->
    <select id="findUserById" parameterType="Integer" resultType="com.school.mybatis.pojo.sysUser">
        select * from sysUser where id = #{v}
    </select>
    <select id="findUserByUsername" parameterType="String" resultType="com.school.mybatis.pojo.sysUser">
        select * from sysuser where sysUsername like ‘%${value}%‘ 
    </select>
    <!-- 
        #{} 占位符
        ${} 字符串拼接 如: select * from sysuser where sysUsername like ‘%${}%‘  ${}等于连接字符串的 ‘+’
     -->
     <insert id="insersysUser" parameterType="com.school.mybatis.pojo.sysUser">
         insert into sysUser (sysUsername,password)
         values (#{SysUserName},#{PassWord})
     
     </insert>
     <update id="UpdateUserById" parameterType="com.school.mybatis.pojo.sysUser">
         update sysUser
         set SysUserName=#{SysUserName},PassWord=#{PassWord}
         where id = #{Id}
         
     </update>
     <delete id="deleteUserById" parameterType="Integer">
         delete from sysUser where id = #{v}
     
     </delete>
</mapper>

 

mybatis 数据库语句

标签:resource   inpu   version   XML   values   class   row   rgb   resources   

原文地址:https://www.cnblogs.com/bxzmd/p/13780470.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!