目录
一、注解开发
1、介绍
2、相关标签、属性介绍
3、步骤
1、在核心配置文件中配置映射关系
2、创建接口 和 查询方法:
3、创建测试类:
二、注解实现多表操作
一对一
1、相关属性 标签介绍
2、实现步骤
一对多
多对多
三、构建SQL语句
四、学生管理系统
一、注解开发
1、介绍
2、相关标签、属性介绍
3、步骤
1、在核心配置文件中配置映射关系
<?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>
<!--引入数据库连接的配置文件-->
<properties resource="jdbc.properties"/>
<!--配置LOG4J-->
<settings>
<setting name="logImpl" value="log4j"/>
</settings>
<!--起别名-->
<typeAliases>
<package name="com.itheima.bean"/>
</typeAliases>
<!-- 配置数据库环境, 环境可以有多个, defult属性是指哪一个 -->
<environments default="mysql">
<environment id="mysql">
<!--transactionManager事务管理。-->
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<!--property获取数据库连接的配置信息-->
<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="com.itheima.mapper"/>
</mappers>
</configuration>
2、创建接口 和 查询方法:
package com.itheima.mapper;
import com.itheima.bean.Student;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
public interface StudentMapper {
/**
* 单表操作
*/
//增
@Insert("insert into student values (#{id}, #{name}, #{age})")
public abstract Integer insert(Student student);
//删
@Delete("delete from student where id=#{id}")
public abstract Integer delete(Integer id);
//改
@Update("update student set name=#{name}, age=#{age} where id=#{id};")
public abstract Integer update(Student student);
//查
@Select("select * from student")
public abstract List<Student> selsctAll();
/**
* 多表操作
*/
}
3、创建测试类:
package com.itheima.test;
import com.itheima.bean.Student;
import com.itheima.mapper.StudentMapper;
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 org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class Test01 {
@Test
public void selectAll() throws IOException {
//1、加载核心配置文件
InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
//2、获取SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3、获取到SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//4、获取StudentMapper接口实现对象
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//5、调用实现类对象中方法,接受结果
List<Student> students = mapper.selsctAll();
//6、处理结果
for (Student student : students) {
System.out.println(student);
}
//7、释放
sqlSession.close();
is.close();
}
@Test
public void insert() throws IOException {
//1、加载核心配置文件
InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
//2、获取SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3、获取到SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//4、获取StudentMapper接口实现对象
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//5、调用实现类对象中方法,接受结果
Student student = new Student(4, "赵六", 15);
Integer result = mapper.insert(student);
//6、处理结果
System.out.println(result);
//7、释放
sqlSession.close();
is.close();
}
@Test
public void update() throws IOException {
//1、加载核心配置文件
InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
//2、获取SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3、获取到SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//4、获取StudentMapper接口实现对象
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//5、调用实现类对象中方法,接受结果
Student student = new Student(4, "赵六", 99);
Integer result = mapper.update(student);
//6、处理结果
System.out.println(result);
//7、释放
sqlSession.close();
is.close();
}
@Test
public void delete() throws IOException {
//1、加载核心配置文件
InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
//2、获取SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3、获取到SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//4、获取StudentMapper接口实现对象
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//5、调用实现类对象中方法,接受结果
Integer id = 2;
Integer result = mapper.delete(id);
//6、处理结果
System.out.println(result);
//7、释放
sqlSession.close();
is.close();
}
}
二、注解实现多表操作
一对一
1、相关属性 标签介绍
2、实现步骤
①创建接口和查询方法
package com.itheima.one_to_one;
import com.itheima.bean.Card;
import com.itheima.bean.Person;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface CardMapper {
//查询全部
@Select("select * from card")
@Results({
@Result(column = "id", property = "id"),
@Result(column = "number", property = "number"),
@Result(
property = "p", //被包含对象的变量名
javaType = Person.class, //被包含对象的实际数据类型
column = "pid", //代表根据查询出的card表中的pid字段来查询person表
/*
one、@One 一对一固定写法
select属性: 指调用哪个接口中的哪个方法
*/
one = @One(select = "com.itheima.one_to_one.PersonMapper.selectById")
)
})
public abstract List<Card> selectAll();
}
package com.itheima.one_to_one;
import com.itheima.bean.Person;
import org.apache.ibatis.annotations.Select;
public interface PersonMapper {
@Select("select * from person where id=#{id}")
public abstract Person selectById(Integer id);
}
?② 配置映射文件
<?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>
<!--引入数据库连接的配置文件-->
<properties resource="jdbc.properties"/>
<!--配置LOG4J-->
<settings>
<setting name="logImpl" value="log4j"/>
</settings>
<!--起别名-->
<typeAliases>
<!--用于指定要配置别名的包,当指定后,该报下的实体类都会注册别名,并且类名就是别名,不再区分大小写-->
<package name="com.itheima.bean"/>
</typeAliases>
<!--集成分页助手插件-->
<!-- <plugins>-->
<!-- <plugin interceptor="com.github.pagehelper.PageInterceptor"/>-->
<!-- </plugins>-->
<!-- 配置数据库环境, 环境可以有多个, defult属性是指哪一个 -->
<environments default="mysql">
<environment id="mysql">
<!--transactionManager事务管理。-->
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<!--property获取数据库连接的配置信息-->
<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="com.itheima.one_to_one"/>
</mappers>
</configuration>
③ 编写测试类
package com.itheima.one_to_one;
import com.itheima.bean.Card;
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 org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class Test01 {
@Test
public void selectAll() throws IOException {
InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
CardMapper mapper = sqlSession.getMapper(CardMapper.class); // 实现类对象
List<Card> list = mapper.selectAll();
for (Card card : list){
System.out.println(card);
}
sqlSession.close();
is.close();
}
}
一对多
多对多
三、构建SQL语句
四、学生管理系统
|