MyBatis接口绑定方案和多参数传递(三)

1.创建JDBC

2.实体类

3.创建接口(类名必须和SQL.XML的类名相同)

4.创建SQL.XML

5.执行代码

1.创建JDBC

<?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="logImpl" value="LOG4J"/>
</settings>
 <typeAliases>
 	<!-- 给返回值起别名 默认类名 -->
 	<package name="com.gx.pojo"/>
 	 <!-- 给返回值起别名可以 自定义名称  -->
 	<!-- <typeAlias type="com.gx.pojo.Y_YuangGong" alias="自定义"/> -->
 	
 </typeAliases>
 
 
	  <environments default="default">
		  <environment id="default">
		  	 <transactionManager type="JDBC"></transactionManager>
		       <dataSource type="POOLED">
		  		<property name="driver" value="com.mysql.jdbc.Driver"/>
				<property name="url" value="jdbc:mysql://localhost:3306/mybas"/>
				<property name="username" value="root"/>
				<property name="password" value="scott"/>
				</dataSource>
		  </environment>
	  </environments>
	  <mappers>
        <!-- 创建连接 -->
	  	<mapper resource="com/gx/dao/LogMapper.xml"/>
	  </mappers>
  </configuration>

2.实体类

public class Account {

	private int accountID;
	private String num;
	private String password;
	private double balance;
	private String name;
	public int getAccountID() {
		return accountID;
	}
	public void setAccountID(int accountID) {
		this.accountID = accountID;
	}
	public String getNum() {
		return num;
	}
	public void setNum(String num) {
		this.num = num;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public double getBalance() {
		return balance;
	}
	public void setBalance(double balance) {
		this.balance = balance;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	@Override
	public String toString() {
		return "Account [accountID=" + accountID + ", num=" + num
				+ ", password=" + password + ", balance=" + balance + ", name="
				+ name + "]";
	}
	
}

 3.创建接口


public interface LogMapper {
	List<Account> selectAccount();
	List<Account> selectAccountByNumPass(String num ,String password);
        //它是把参数转成Map集合,@Param("nun")是键-nun111是值;
        //使用在sql.xml中写键来取值,和nun111命名无关;
	List<Account> selectAccountByNumPassOne(@Param("nun") String nun111,@Param("password") String password111);
}

      方法名称必须和sql.xml的id对应;

4.创建SQL.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">
<!--LogMapper必须和接口类目相同-->
  <mapper namespace="com.gx.dao.LogMapper">
 <!--id必须对应接口方法名称-->
  	<select id="selectAccount" resultType="Account">
  	select * from account
  	</select>
  	<!--不需要参数类型-->
  	<select id="selectAccountByNumPass" resultType="Account">
  	select * from account where num=#{0} and password=#{1}
  	<!-- select * from Y_YuangGong where YuanGongDaiMa=#{Param1} and MiMa=#{Param2} -->
  	
  	</select>
  	<!--跟@param("")的命名相同-->
  	<select id="selectAccountByNumPassOne" resultType="Account">
  	select * from account where num=#{nun} and password=#{password}
  	</select>
  </mapper>

5.执行代码

	public static void main(String[] args) throws IOException {
               //数据库连接属性
		InputStream resourceAsStream = Resources.getResourceAsStream("GlobalConfiguration.xml");
               //连接工厂
		SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
                //打开数据库
		SqlSession openSession = build.openSession();
                //实例接口
		LogMapper mapper = openSession.getMapper(LogMapper.class);
                //通过实体接口直接调取出接口里面的方法
		//查询所有
		List<Account> selectAccount = mapper.selectAccount();
		for (Account account : selectAccount) {
			System.out.println(account.toString());
		}
		//条件查询
		List<Account> selectAccountByNumPass = mapper.selectAccountByNumPass("1", "1");
		for (Account account : selectAccountByNumPass) {
			System.out.println(account.toString());
		}
		//指定参数名
		List<Account> selectAccountByNumPassOne = mapper.selectAccountByNumPassOne("2", "2");
		for (Account account : selectAccountByNumPassOne) {
			System.out.println(selectAccountByNumPassOne.toString());
		}
			
}

注意:

1.创建接口类名和SQL.XML类名必须相同;(public interface LogMapper 和 namespace="com.gx.dao.LogMapper")

2.接口方法名称和对应需要使用的SQL语句的ID相同;( id="selectAccount"  相同  List<Account> selectAccount();)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis是一个Java持久层框架,提供了方便的数据库访问和映射功能。在MyBatis中,可以使用批处理操作来提高数据库操作的效率。 批处理操作是指一次性执行多个SQL语句,而不是逐条执行。这在需要执行大量相似的SQL语句时非常有用,比如插入或更新多条记录。 在MyBatis中,可以使用`SqlSession`对象的`insert(String statement, Object parameter)`、`update(String statement, Object parameter)`和`delete(String statement, Object parameter)`方法来执行批处理操作。 要执行批处理操作,可以将多个参数对象放入一个List中,然后将这个List作为参数传递给上述方法。MyBatis会自动将每个参数对象到对应的SQL语句中,然后一次性执行所有的SQL语句。 以下是一个使用MyBatis进行批处理操作的示例: ```java List<User> userList = new ArrayList<>(); // 假设有多个User对象需要插入 // 创建SqlSession对象 SqlSession sqlSession = sqlSessionFactory.openSession(); try { // 遍历User列表,执行插入操作 for (User user : userList) { sqlSession.insert("insertUser", user); } // 提交事务 sqlSession.commit(); } finally { // 关闭SqlSession sqlSession.close(); } ``` 在上述示例中,`insertUser`是一个MyBatis映射文件中义的插入语句,用于将User对象插入数据库。 通过使用批处理操作,可以减少与数据库的交互次数,提高数据插入或更新的效率。 需要注意的是,批处理操作可能对内存和数据库性能产生一的压力,因此在使用批处理操作时需谨慎考虑数据量和系统资源。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值