欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java脚本注释格式(java的三种注释类型)

程序员文章站 2023-11-28 13:25:52
mybatis动态sqlmybatis,在带注解的映射器接口类中使用动态sql,可使用script元素,即:。案例代码modelpackage...

mybatis动态sql

mybatis,在带注解的映射器接口类中使用动态sql,可使用script元素,即:<script></script>。

java脚本注释格式(java的三种注释类型)

案例代码

model

package com.what21.mybatis.model;

import lombok.data;

@data
public class employee {

    // id
    private integer id;
    // 名称
    private string name;
    // 邮箱
    private string email;
    // 性别
    private string gender;
    // 年龄
    private integer age;

}

mapper<映射器接口>

package com.what21.mybatis.demo03.mapper;

import com.what21.mybatis.model.employee;
import org.apache.ibatis.annotations.*;

import java.util.list;

public interface employeemapper {

    @select(value = "select * from `employee` where id = #{id}")
    @resulttype(employee.class)
    public employee findemployeebyid(integer id);

    @update(value = "<script>" +
            "update `employee` " +
            "   <set>" +
            "       <if test='name != null'>" +
            "           `name`=#{name}," +
            "       </if>" +
            "       <if test='email != null'>" +
            "            `email`=#{email}," +
            "       </if>" +
            "       <if test='gender != null'>" +
            "             `gender`=#{gender}," +
            "       </if>" +
            "       <if test='age != null'>" +
            "            `age`=#{age}  " +
            "       </if>" +
            "   </set>" +
            "where id = #{id}" +
            "</script>")
    public int updateemployee(employee employee);

    @delete(value = "<script>" +
            "   delete from `employee` where id in" +
            "       <foreach collection='idlist' item='id' open='(' separator=',' close=')'>" +
            "           #{id}" +
            "       </foreach>" +
            "</script>")
    public int deleteemployeebyids(@param("idlist") list<integer> idlist);

}

配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<!doctype configuration
        public "-//mybatis.org//dtd config 3.0//en"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <settings>
        <!--建议显示的指定需要配置的值,防止版本更新带来的问题-->
        <!--开启驼峰命名规则-->
        <setting name="mapunderscoretocamelcase" value="true"/>
    </settings>

    <typealiases>
        <typealias type="com.what21.mybatis.model.employee" alias="employee"/>
    </typealiases>

    <plugins>
        <plugin interceptor="com.github.pagehelper.pageinterceptor">
            <!--默认值为 false,当该参数设置为 true 时,如果 pagesize=0 或者 rowbounds.limit = 0 就会查询出全部的结果-->
            <!--如果某些查询数据量非常大,不应该允许查出所有数据-->
            <property name="pagesizezero" value="true"/>
        </plugin>
    </plugins>

    <environments default="development">
        <environment id="development">
            <transactionmanager type="jdbc"/>
            <datasource type="pooled">
                <property name="driver" value="com.mysql.jdbc.driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/demo?characterencoding=utf8&usessl=false&servertimezone=utc&rewritebatchedstatements=true"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </datasource>
        </environment>
    </environments>

    <mappers>
        <mapper class="com.what21.mybatis.demo03.mapper.employeemapper" />
    </mappers>

</configuration>

测试类<main方法测试>:

package com.what21.mybatis.demo03;

import com.what21.mybatis.demo03.mapper.employeemapper;
import com.what21.mybatis.model.employee;
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 java.io.ioexception;
import java.io.reader;
import java.util.arrays;
import java.util.list;

public class mybatisdemomain {

    /**
     * 创建sessionfactory
     *
     * @param mybatisconfig
     * @return
     */
    private static sqlsessionfactory getsessionfactory(string mybatisconfig) {
        sqlsessionfactory sessionfactory = null;
        try {
            reader reader = resources.getresourceasreader(mybatisconfig);
            sessionfactory = new sqlsessionfactorybuilder().build(reader);
        } catch (ioexception e) {
            e.printstacktrace();
        }
        return sessionfactory;
    }

    public static void main(string[] args) {
        string resource = "com/what21/mybatis/demo03/mybatisconfig.xml";
        // 从sessionfactory中获取sqlsession
        sqlsession sqlsession = getsessionfactory(resource).opensession();
        // mapper
        employeemapper employeemapper = sqlsession.getmapper(employeemapper.class);
        // select操作
        employee findemployee = employeemapper.findemployeebyid(2);
        system.out.println(findemployee);
        findemployee.setname("学习测试demo");
        findemployee.setemail("leran@demo.com");
        findemployee.setage(34);
        findemployee.setgender("1");
        int updateoperate = employeemapper.updateemployee(findemployee);
        system.out.println("update操作结果:" + updateoperate);
        list<integer> idslist = arrays.aslist(findemployee.getid());
        // delete操作
        int deleteoperate = employeemapper.deleteemployeebyids(idslist);
        // sql正确,没有删除任何一条数据,返回结果为:0
        system.out.println("delete操作结果:" + deleteoperate);
        // 提交
        //sqlsession.commit();
    }

}