sql server 与 mybatis的简单使用

例:

表名  Emploree

字段  id, name, sex, dept, idcard, license, photo

  

1、查询全部方式一

select
id, name, sex, dept, idcard, license, photo
from Emploree
where 1=1

 

2、查询全部方式二

select * from Emploree;

 

3、根据指定字段查询

select
id, name, sex, dept, idcard, license, photo
from Emploree
where 1=1 and idcard=421544846849

 

4、批量插入

<insert id="insertList" parameterType="java.util.ArrayList">
    INSERT INTO Emploree (name,sex,dept, idcard,license,photo) values
    <foreach collection="list" item="item" index="index" separator=",">
    (
        #{item.name,jdbcType=VARCHAR}, 
        #{item.sex,jdbcType=TINYINT}, 
        #{item.dept,jdbcType=TINYINT}, 
        #{item.idcard,jdbcType=CHAR}, 
        #{item.license,jdbcType=VARCHAR}, 
        #{item.photo,jdbcType=TINYINT}
    )
    </foreach>
</insert>
注意:  批量插入时,list中存放的自定义javaBean对象(一般是对应数据库表的实体类),
    
    在进行sql语句拼接的时候可直接使用 [对象].[属性名] 的方式进行拼接
      例: item.name    [正确写法]

    而不是调用对象的get方法,
      例: item.getName() [错误写法]

 

5、删除表的全部数据,但保留表结构,同时id从1开始自增

truncate table Emploree

 

6、插入单条记录

insert into Emploree 
  (id, name, sex, dept, idcard, license, photo)
  values (#{id},#{name},...)

 

7、根据姓名进行模糊查询

例:查询所有和  ‘二’  相关的名字

  查询结果:二哈、小二、王二小

select * from Emploree 
    where 
        name like '%'+'二'+'%'    

 

posted @ 2018-07-28 17:11  民院小青年  阅读(1697)  评论(0编辑  收藏  举报