<?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">
<mapper namespace="com.song.repository.AccountRepository">
<insert id="save" parameterType="com.song.entity.Account">
insert into mybatis.t_account(id,username,password,age) values(#{id},#{username},#{password},#{age})
</insert>
<update id="update" parameterType="com.song.entity.Account">
update mybatis.t_account set username = #{username},password=#{password},age=#{age} where id=#{id}
</update>
<delete id="delete" parameterType="long">
delete from mybatis.t_account where id=#{id};
</delete>
<select id="findAll" resultType="com.song.entity.Account">
select * from mybatis.t_account;
</select>
<select id="findById" parameterType="long" resultType="com.song.entity.Account">
select * from mybatis.t_account where id=#{id}
</select>
<select id="findByName" parameterType="java.lang.String"
resultType="com.song.entity.Account">
select * from mybatis.t_account where username = #{username}
</select>
<select id="findById2" parameterType="java.lang.Long"
resultType="com.song.entity.Account">
select * from mybatis.t_account where id = #{id}
</select>
<select id="findByNameAndAge" resultType="com.song.entity.Account">
select * from mybatis.t_account where username = #{arg0} and age = #{arg1}
</select>
</mapper>
package com.song.repository;
import com.song.entity.Account;
import java.util.List;
public interface AccountRepository {
public int save(Account account);
public int update(Account account);
public int delete(long id);
public List<Account> findAll();
public Account findById(long id);
public Account findByName(String username);
public Account findById2(Long id);
public Account findByNameAndAge(String name,int age);
}
package com.song.Test;
import com.song.entity.Account;
import com.song.repository.AccountRepository;
import org.apache.ibatis.io.ResolverUtil;
import org.apache.ibatis.session.sqlSession;
import org.apache.ibatis.session.sqlSessionFactory;
import org.apache.ibatis.session.sqlSessionFactoryBuilder;
import org.junit.Test;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class test2 {
public static void main(String[] args) {
//调⽤接⼝的代理对象完成相关的业务操作
InputStream inputStream = Test.class.getClassLoader().getResourceAsstream("config.xml");
sqlSessionFactoryBuilder sqlSessionFactoryBuilder = new sqlSessionFactoryBuilder();
sqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
sqlSession sqlSession = sqlSessionFactory.openSession();
AccountRepository accountRepository = sqlSession.getMapper(AccountRepository.class);
//添加对象
// Account account = new Account(5L,"王五","111",223);
// accountRepository.save(account);
// sqlSession.commit();
// sqlSession.close();
// 查询全部对象
// List<Account> list = accountRepository.findAll();
// for (Account account1:
// list) {
// System.out.println(account1);
// }
// sqlSession.close();
// int result = accountRepository.delete(3L);
// System.out.println(result);
// sqlSession.commit();
// sqlSession.close();
// Account byId = accountRepository.findById(1L);
// System.out.println(byId);
// sqlSession.close();
// List<Account> list = accountRepository.findAll();
// for (Account account:
// list) {
// System.out.println(account);
// }
// sqlSession.close();
//修改
// Account account = accountRepository.findById(1L);
// account.setAge(176);
// account.setUsername("邹松林");
// account.setPassword("2739sd");
// int update = accountRepository.update(account);
// System.out.println(update);
// sqlSession.commit();
// sqlSession.close();
//通过string类 通过name查询
// Account account = accountRepository.findByName("邹松林");
// System.out.println(account);
//包装类,通过 id 查询 Account
// Account byId2 = accountRepository.findById2(1L);
// System.out.println(byId2);
//4、多个参数,通过 name 和 age 查询 Account
Account account = accountRepository.findByNameAndAge("邹松林", 176);
System.out.println(account);
}
}
package com.song.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Account {
private long id;
private String username;
private String password;
private int age;
}
复制
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。