⾃定义接⼝,定义相关业务⽅法。
编写与⽅法相对应的 Mapper.xml。
1、⾃定义接⼝
2、创建接⼝对应的 Mapper.xml,定义接⼝⽅法对应的 SQL 语句。
<mappers>
<mapper resource="com/southwind/mapper/AccountMapper.xml"></mapper>
</mappers>
</configuration>
public class Test {
public static void main(String[] args) {
//加载MyBatis配置⽂件
InputStream inputStream =
Test.class.getClassLoader().getResourceAsStream("config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new
SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory =
sqlSessionFactoryBuilder.build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
String statement = "com.southwind.mapper.AccoutMapper.save";
Account account = new Account(1L,"张三","123123",22);
sqlSession.insert(statement,account);
sqlSession.commit();
}
}
package com.southwind.repository;
import com.southwind.entity.Account;
import java.util.List;
public interface AccountRepository {
public int save(Account account);
public int update(Account account);
public int deleteById(long id);
public List<Account> findAll();
public Account findById(long id);
}
statement 标签可根据 SQL 执⾏的业务选择 insert、delete、update、select。
MyBatis 框架会根据规则⾃动创建接⼝实现类的代理对象。
规则:
Mapper.xml 中 namespace 为接⼝的全类名。
Mapper.xml 中 statement 的 id 为接⼝中对应的⽅法名。
Mapper.xml 中 statement 的 parameterType 和接⼝中对应⽅法的参数类型⼀致。
Mapper.xml 中 statement 的 resultType 和接⼝中对应⽅法的返回值类型⼀致。
3、在 config.xml 中注册 AccountRepository.xml
4、调⽤接⼝的代理对象完成相关的业务操作
<?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.southwind.repository.AccountRepository">
<insert id="save" parameterType="com.southwind.entity.Account">
insert into t_account(username,password,age) values(#{username},#
{password},#{age})
</insert>
<update id="update" parameterType="com.southwind.entity.Account">
update t_account set username = #{username},password = #{password},age
= #{age} where id = #{id}
</update>
<delete id="deleteById" parameterType="long">
delete from t_account where id = #{id}
</delete>
<select id="findAll" resultType="com.southwind.entity.Account">
select * from t_account
</select>
<select id="findById" parameterType="long"
resultType="com.southwind.entity.Account">
select * from t_account where id = #{id}
</select>
</mapper>
<!-- 注册AccountMapper.xml -->
<mappers>
<mapper resource="com/southwind/mapper/AccountMapper.xml"></mapper>
<mapper resource="com/southwind/repository/AccountRepository.xml"></mapper>
</mappers>
package com.southwind.test;
import com.southwind.entity.Account;
import com.southwind.repository.AccountRepository;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.InputStream;
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(3L,"王五","111111",24);
// int result = accountRepository.save(account);
// sqlSession.commit();
//查询全部对象
// List<Account> list = accountRepository.findAll();
// for (Account account:list){
// System.out.println(account);
// }
// sqlSession.close();
//通过id查询对象
// Account account = accountRepository.findById(3L);
// System.out.println(account);
// sqlSession.close();
//修改对象
// Account account = accountRepository.findById(3L);
// account.setUsername("⼩明");
// account.setPassword("000");
// account.setAge(18);
// int result = accountRepository.update(account);
// sqlSession.commit();
// System.out.println(result);
// sqlSession.close();
//通过id删除对象
int result = accountRepository.deleteById(3L);
System.out.println(result);
sqlSession.commit();
sqlSession.close();
}
}
Mapper.xml
statement 标签:select、update、delete、insert 分别对应查询、修改、删除、添加操作。
parameterType:参数数据类型
1、基本数据类型,通过 id 查询 Account
2、String 类型,通过 name 查询 Account
3、包装类,通过 id 查询 Account
4、多个参数,通过 name 和 age 查询 Account
5、Java Bean
resultType:结果类型
1、基本数据类型,统计 Account 总数
<select id="findById" parameterType="long"
resultType="com.southwind.entity.Account">
select * from t_account where id = #{id}
</select>
<select id="findByName" parameterType="java.lang.String"
resultType="com.southwind.entity.Account">
select * from t_account where username = #{username}
</select>
<select id="findById2" parameterType="java.lang.Long"
resultType="com.southwind.entity.Account">
select * from t_account where id = #{id}
</select>
<select id="findByNameAndAge" resultType="com.southwind.entity.Account">
select * from t_account where username = #{arg0} and age = #{arg1}
</select>
<update id="update" parameterType="com.southwind.entity.Account">
update t_account set username = #{username},password = #{password},age =
#{age} where id = #{id}
</update>
<select id="count" resultType="int">
select count(id) from t_account
</select>
2、包装类,统计 Account 总数
3、String 类型,通过 id 查询 Account 的 name
4、Java Bean
及联查询
⼀对多
Student
package com.southwind.entity;
import lombok.Data;
@Data
public class Student {
private long id;
private String name;
private Classes classes;
}
Classes
<select id="count2" resultType="java.lang.Integer">
select count(id) from t_account
</select>
<select id="findNameById" resultType="java.lang.String">
select username from t_account where id = #{id}
</select>
<select id="findById" parameterType="long"
resultType="com.southwind.entity.Account">
select * from t_account where id = #{id}
</select>
```java
```java
package com.southwind.entity;
import lombok.Data;
import java.util.List;
@Data
public class Classes {
private long id;
private String name;
private List<Student> students;
}
StudentRepository
package com.southwind.repository;
import com.southwind.entity.Student;
public interface StudentRepository {
public Student findById(long id);
}
StudentRepository.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">
<mapper namespace="com.southwind.repository.StudentRepository">
<resultMap id="studentMap" type="com.southwind.entity.Student">
<id column="id" property="id"></id>
<result column="name" property="name"></result>
<association property="classes" javaType="com.southwind.entity.Classes">
<id column="cid" property="id"></id>
<result column="cname" property="name"></result>
</association>
</resultMap>
<select id="findById" parameterType="long" resultMap="studentMap">
select s.id,s.name,c.id as cid,c.name as cname from student s,classes c
where s.id = #{id} and s.cid = c.id
</select>
</mapper>
ClassesRepository
package com.southwind.repository;
import com.southwind.entity.Classes;
public interface ClassesRepository {
public Classes findById(long id);
}
ClassesRepository.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">
<mapper namespace="com.southwind.repository.ClassesRepository">
<resultMap id="classesMap" type="com.southwind.entity.Classes">
<id column="cid" property="id"></id>
<result column="cname" property="name"></result>
<collection property="students" ofType="com.southwind.entity.Student">
<id column="id" property="id"/>
<result column="name" property="name"/>
</collection>
</resultMap>
<select id="findById" parameterType="long" resultMap="classesMap">
select s.id,s.name,c.id as cid,c.name as cname from student s,classes c
where c.id = #{id} and s.cid = c.id
</select>
</mapper>
多对多
Customer
package com.southwind.entity;
import lombok.Data;
import java.util.List;
@Data
public class Customer {
private long id;
private String name;
private List<Goods> goods;
}
Goods
package com.southwind.entity;
import lombok.Data;
import java.util.List;
@Data
public class Goods {
private long id;
private String name;
private List<Customer> customers;
}
CustomerRepository
package com.southwind.repository;
import com.southwind.entity.Customer;
public interface CustomerRepository {
public Customer findById(long id);
}
CustomerRepository.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">
<mapper namespace="com.southwind.repository.CustomerRepository">
<resultMap id="customerMap" type="com.southwind.entity.Customer">
<id column="cid" property="id"></id>
<result column="cname" property="name"></result>
<collection property="goods" ofType="com.southwind.entity.Goods">
<id column="gid" property="id"/>
<result column="gname" property="name"/>
</collection>
</resultMap>
<select id="findById" parameterType="long" resultMap="customerMap">
select c.id cid,c.name cname,g.id gid,g.name gname from customer c,goods
g,customer_goods cg where c.id = #{id} and cg.cid = c.id and cg.gid = g.id
</select>
</mapper>
GoodsRepository
package com.southwind.repository;
import com.southwind.entity.Goods;
public interface GoodsRepository {
public Goods findById(long id);
}
GoodsRepository.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">
<mapper namespace="com.southwind.repository.GoodsRepository">
<resultMap id="goodsMap" type="com.southwind.entity.Goods">
<id column="gid" property="id"></id>
<result column="gname" property="name"></result>
<collection property="customers" ofType="com.southwind.entity.Customer">
<id column="cid" property="id"/>
<result column="cname" property="name"/>
</collection>
</resultMap>
<select id="findById" parameterType="long" resultMap="goodsMap">
select c.id cid,c.name cname,g.id gid,g.name gname from customer c,goods
g,customer_goods cg where g.id = #{id} and cg.cid = c.id and cg.gid = g.id
</select>
</mapper>