首页 > 其他分享 >mybatis动态方法

mybatis动态方法

时间:2023-10-12 19:31:50浏览次数:36  
标签:name people addres People log4j mybatis 动态 方法 public

一、首先创建数据库

mybatis动态方法_sql

二、创建项目工程

mybatis动态方法_java_02

先导入文件

mybatis动态方法_java_03

编写配置文件

mybatis动态方法_sql_04

db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/db
username=root
password=root
config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "mybatis-3-config.dtd" >
<configuration>
<properties resource="db.properties"></properties>

<!-- 开启复杂的自动映射 -->
<!-- <settings>
<setting name="autoMappingBehavior" value="FULL"/>
</settings> -->

<typeAliases>
<package name="cn.lexed.pojo"/>
</typeAliases>

<environments default="dev">
<environment id="dev">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>

<mappers>
<package name="cn.lexed.dao"/>
</mappers>
</configuration>
log4j.properties
log4j.rootLogger=DEBUG,CONSOLE,file
#log4j.rootLogger=ERROR,ROLLING_FILE
log4j.logger.cn.lexed.dao=debug
log4j.logger.com.ibatis=debug 
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug 
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug 
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug 
log4j.logger.java.sql.Connection=debug 
log4j.logger.java.sql.Statement=debug 
log4j.logger.java.sql.PreparedStatement=debug 
log4j.logger.java.sql.ResultSet=debug 
log4j.logger.org.tuckey.web.filters.urlrewrite.UrlRewriteFilter=debug

######################################################################################
# Console Appender  \u65e5\u5fd7\u5728\u63a7\u5236\u8f93\u51fa\u914d\u7f6e
######################################################################################
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.Threshold=error
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern= [%p] %d %c - %m%n


######################################################################################
# DailyRolling File  \u6bcf\u5929\u4ea7\u751f\u4e00\u4e2a\u65e5\u5fd7\u6587\u4ef6\uff0c\u6587\u4ef6\u540d\u683c\u5f0f:log2009-09-11
######################################################################################
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.DatePattern=yyyy-MM-dd
log4j.appender.file.File=log.log
log4j.appender.file.Append=true
log4j.appender.file.Threshold=error
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-M-d HH:mm:ss}%x[%5p](%F:%L) %m%n


log4j.logger.com.opensymphony.xwork2=error  

导入工具包

mybatis动态方法_apache_05

MybatisUtils.java
package cn.lexed.utils;

import java.io.InputStream;

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

public class MybatisUtils {
	
	      private static SqlSessionFactory factory;
	      
	      static{  //在静态代码块里,factory只会被创建一次
	    	  try{
	    		 InputStream is=Resources.getResourceAsStream("config.xml");
	    		 factory=new SqlSessionFactoryBuilder().build(is);
	    	  }catch(Exception e){
	    		  e.printStackTrace();
	    	  }
	    	  
	      }
	      
	      //获取SqlSession
	      public static SqlSession createSqlSession(){
	    	  
	    	  return  factory.openSession(true); //true为自动提交   
	      }
	      
	      
	      
	      public static void closeSqlSession(SqlSession sqlSession){
	      	  if(null!=sqlSession){
	    		  sqlSession.close();
	    	  }
	      }
}

创建People类

mybatis动态方法_java_06

People.java

package cn.lexed.pojo;

public class People {
	private Integer id;
	private String name;
	private Integer age;
	private String addres;
	
	public People() {
		super();
	}

	public People(Integer id, String name, Integer age, String addres) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
		this.addres = addres;
	}

	public Integer getId() {
		return id;
	}


	public void setId(Integer id) {
		this.id = id;
	}


	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public Integer getAge() {
		return age;
	}


	public void setAge(Integer age) {
		this.age = age;
	}


	public String getAddres() {
		return addres;
	}


	public void setAddres(String addres) {
		this.addres = addres;
	}


	@Override
	public String toString() {
		return "People [id=" + id + ", name=" + name + ", age=" + age + ", addres=" + addres + "]";
	}
	
}

编写动态增、删、改、查方法

mybatis动态方法_apache_07

PeopleMapper.java
package cn.lexed.dao;

import cn.lexed.pojo.People;

public interface PeopleMapper {
    public People getPeopleById(People people);//查询
	
	public void updatePeople(People people);//修改
	
	public void insertPeople(People people);//添加
	
	public void deletePeople(People people);//删除
}
PeopleMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="cn.lexed.dao.PeopleMapper">

<!-- 查询 -->
<select id="getPeopleById" parameterType="people" resultType="people">
select * from people
<where>
<if test="name!=null and name!=''">
name=#{name}
</if>
<if test="addres!=null and addres!=''">
and addres=#{addres}
</if>
</where>
</select>

<!-- 修改 -->
<update id="updatePeople" parameterType="people">
update people
<set>
<if test="name!=null and name!=''">
name=#{name},
</if>
<if test="age!=null and age!=''">
age=#{age},
</if>
<if test="addres!=null and addres!=''">
addres=#{addres}
</if>
</set>
<where>
<if test="id!=null">
id=#{id}
</if>
</where>
</update>

<!-- 添加 -->
<sql id="key">
<trim suffixOverrides=",">
<if test="name!=null and name!=''">name,</if>
<if test="age!=null and age!=''">age,</if>
<if test="addres!=null and addres!=''">addres,</if>
</trim>
</sql>

<sql id="value">
<trim suffixOverrides=",">
<if test="name!=null and name!=''">#{name},</if>
<if test="age!=null and age!=''">#{age},</if>
<if test="addres!=null and addres!=''">#{addres},</if>
</trim>
</sql>

<insert id="insertPeople" parameterType="people">
insert into people(<include refid="key"/>) values(<include refid="value"/>)
</insert>

<!-- 删除 -->
<delete id="deletePeople" parameterType="people">
delete from people
<where>
<if test="id!=null">
id=#{id}
</if>
</where>
</delete>

</mapper>

编写测试类

mybatis动态方法_java_08

Test(Jun)
package cn.lexed.test;

import static org.junit.Assert.*;

import org.apache.ibatis.session.SqlSession;

import cn.lexed.dao.PeopleMapper;
import cn.lexed.pojo.People;
import cn.lexed.utils.MybatisUtils;

public class Test {

	@org.junit.Test
	public void test(){
		//1.获取SqlSession对象
		SqlSession s=MybatisUtils.createSqlSession();
		People people=new People();
		people.setName("马云");
		people.setAddres("杭州");
		People peo=s.getMapper(PeopleMapper.class).getPeopleById(people);
		System.out.println(peo);
	}
	@org.junit.Test
	public void test1(){
		//1.获取SqlSession对象
		SqlSession s=MybatisUtils.createSqlSession();
		People people=new People();
		people.setName("马云");
		people.setAge(18);
		people.setAddres("深圳");
		people.setId(5);
		s.getMapper(PeopleMapper.class).updatePeople(people);
	}
	@org.junit.Test
	public void test2(){
		//1.获取SqlSession对象
		SqlSession s=MybatisUtils.createSqlSession();
		People people=new People();
		people.setName("李嘉诚");
		people.setAge(95);
		people.setAddres("香港");
		s.getMapper(PeopleMapper.class).insertPeople(people);
	}
	@org.junit.Test
	public void test3(){
		//1.获取SqlSession对象
		SqlSession s=MybatisUtils.createSqlSession();
		People people=new People();
		people.setId(29);
		s.getMapper(PeopleMapper.class).deletePeople(people);
	}

}


标签:name,people,addres,People,log4j,mybatis,动态,方法,public
From: https://blog.51cto.com/u_16220922/7834862

相关文章

  • 动态规划——树形DP 学习笔记
    动态规划——树形DP学习笔记引入前置知识:树基础。树形DP,即在树上进行的DP,最常见的状态表示为\(f_{u,\cdots}\),表示以\(u\)为根的子树的某个东东。本文将讲解一些经典题目(树的子树个数、树的最大独立集、树的最小点覆盖、树的最小支配集、树的直径、树的重心、树的中心),以......
  • 【安规测试之接地阻抗测试方法】
    接地阻抗测试:使用25A的电流,测试外壳到PE端的电阻要小于100mΩ使用工具:接地阻抗测试仪(如下图) 注意事项:1、强电流,要带绝缘手套操作。2、测试时别靠近触摸产品仪器。测试步骤:按下图进行接线,黑线连接产品PE端,红线固定在产品外壳金属部分,在接地阻抗测试仪上设置好25A电流,上限为100mΩ......
  • 实时美妆魅力尽显:探索相芯轻美妆SDK功能及集成方法(Android)
    随着人工智能技术的不断发展,相芯SDK的轻美妆功能为用户带来了一种全新的美妆体验。轻美妆是妆容和美妆的结合,使用比较轻薄的妆效使轻美妆效果更佳。这一功能集成了多项先进技术,通过人脸识别、图像处理和分割等技术,实现了实时美妆效果的展示和应用。本文将深入介绍相芯SDK轻美妆功能......
  • Java8新特性之方法引用(三)
    1.方法引用介绍方法引用其实是lambda表达式的升级写法,采用::固定语法,可以使代码更简洁、紧凑;2.方法引用的5种类型2.1对象引用::实例方法名函数式接口抽象方法的形参列表与实例方法的一致,且方法的返回值类型一致;publicclassMethodReferencesTest{publicstaticvo......
  • Java8新特性之接口的默认方法和静态方法(四)
    1.背景介绍在Java8之前,接口中定义的方法都是抽象方法,即默认都是被publicabstract修饰的;但从Java8开始,允许在接口中定义带有方法体的默认方法和静态方法;publicclassInterfaceTest{publicstaticvoidmain(String[]args){}}interfaceInterfaceA{/*......
  • java项目使用Mybatis-Plus插件,QueryWrapper日期开始-结束范围查询
    1、参数开始日期startTime、结束日期endTime挺好用,开始日期、结束日期当天都包含进去了,如果使用qw.between("create_time",startTime,endTime)方法是不含endTime结束日期当天的qw.apply(bCulresCardMvVO.getStartTime()!=null,"date_format(create_time,......
  • vue中下载excel文件4种方法,2、通过 a 标签 download 属性结合 blob 构造函数下载发送p
    vue中下载excel文件4种方法,2、通过a标签download属性结合blob构造函数下载发送post请求和后台poi返回文件流实现下载1、通过url下载即后端提供文件的地址,直接使用浏览器去下载通过window.location.href=文件路径下载window.location.href=`${location.origin}/opera......
  • 内网环境中获取域控权限的常见方法
    内网协议NTLM-CVE-2019-1040利用1-exchange​通过printerbug使exchange机器向攻击机发送认证回连攻击者的机器,再使用ntlmrelayx--remove-mic参数绕过NTLMMIC校验,将smb中继到LDAP进行身份认证。​打印机的spoolsv.exe服务允许用户远程调用,当用户向RemoteFindFirstPrinterChan......
  • Vue报错Syntax Error:TypeError: this.getOptions is not a function的解决方法~
    前几天在vue运行项目过程中报错了,这个方法是关于Vue报错SyntaxError:TypeError:this.getOptionsisnotafunction的解决方法(1)报错一(2)报错二~1.1问题分析首先,检查代码,并没有什么错误的地方;其次,涉及到这个问题,可能就是版本原因了,安装的sass-loader版本太高,卸载安装低......
  • 【leaflet】动态轨迹
    使用插件:leaflet.motionhttps://github.com/Igor-Vladyka/leaflet.motion//执行轨迹回放doShowTraces(latLngs){latLngs=[[39.123595,110.561176],[39.123041,110.566147],[39.116309,110.566799],[39.115508,110.......