1.0版本 sql映射文件实现
流程
首先程序进入启动类MyBatisDemo.java中,读取配置文件mybatis-config.xml
再由mybatis-config的mappers属性
<mappers><mapper resource="UserMapper.xml"></mapper></mappers>
找到sql映射文件UserMapper.xml文件,通过其中的namespace属性,
<mapper namespace="test">
找到User类,并和数据库表做映射。
项目结构
User.java
package com.itheima.pojo;public class User {String id;public String getId() {return id;}public void setId(String id) {this.id = id;}@Overridepublic String toString() {return "User{" +"id='" + id + '\'' +'}';}
}
MyBatisDemo.java
package com.itheima;import com.itheima.pojo.User;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List;public class MyBatisDemo {public static void main(String[] args) throws IOException {// 加载mybatis核心配置文件,获取SqlSessionFactoryString resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);// SqlSession,用它执行sqlSqlSession sqlSession = sqlSessionFactory.openSession();// 执行sqlList<User> users = sqlSession.selectList("test.selectAll");System.out.println(users);// 释放资源sqlSession.close();}}
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><!-- environments default属性可以切换不同的environment --><environments default="development"><environment id="development"><transactionManager type="JDBC"></transactionManager><dataSource type="POOLED"><!-- 数据库连接信息 --><property name="driver" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql:///mybatis?useSSL=false"></property><property name="username" value="root"></property><property name="password" value="1234"></property></dataSource></environment></environments><mappers><mapper resource="UserMapper.xml"></mapper></mappers>
</configuration>
这里的
<mappers><mapper resource="UserMapper.xml"></mapper></mappers>
是因为UserMapper和mybatis-config同处于一个包,所以可以只写名字。但是如果UserMapper不和其同处于一个包的话,就需要写全路径。
UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="test"><select id = "selectAll" resultType ="com.itheima.pojo.User">select *from tb_user;</select></mapper>
这里的namespace
<mapper namespace="test">
相当于这个mapper文件的唯一标识,不能与其他mapper文件的重复,就相当于外面套了一个test包,在其他地方调用,譬如启动类MyBatisDemo.java中调用时,要用test.方法名。
2.0版本 mapper代理开发
注意
想在resources包下面创建多级目录,譬如"com.itheima.mapper",不能直接创建一个目录,然后名称写"com.itheima.mapper",这样只会默认创建一个"com.itheima.mapper"的目录,而不会自动分级。
如果想要自动分级,需要写"com/itheima/mapper"
整体流程
首先程序进入启动类MyBatisDemo中,读取配置文件mybatis-config.xml
再由mybatis-config的mappers属性
<mappers><package name="com.itheima.mapper"/></mappers>
找到mapper包下面的mapper接口。
因为mapper接口和sql映射文件属于同一个包层次结构,所以由mapper接口可以找到sql映射文件UserMapper.xml。再由其中的resultType字段
<select id = "selectAll" resultType ="com.itheima.pojo.User">
找到pojo类,并映射对应的数据库表结构。
项目结构
mapper包下的UserMapper.java
package com.itheima.mapper;import com.itheima.pojo.User;·54
import java.util.List;public interface UserMapper {List<User> selectAll();
}
pojo包下的User类
package com.itheima.pojo;public class User {String id;public String getId() {return id;}public void setId(String id) {this.id = id;}@Overridepublic String toString() {return "User{" +"id='" + id + '\'' +'}';}
}
UserMapper.xml
namespace为mapper接口的全限定名。
可以右键点击mapper接口,复制引用,即其全限定名。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.mapper.UserMapper"><select id = "selectAll" resultType ="com.itheima.pojo.User">select *from tb_user;</select></mapper>
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><environments default="development"><environment id="development"><transactionManager type="JDBC"></transactionManager><dataSource type="POOLED"><!-- 数据库连接信息 --><property name="driver" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql:///mybatis?useSSL=false"></property><property name="username" value="root"></property><property name="password" value="1234"></property></dataSource></environment></environments><mappers><mapper resource="com/itheima/mapper/UserMapper.xml"<package name="com.itheima.mapper"/></mappers>
</configuration>
MyBatisDemo
package com.itheima;import java.io.IOException;
import java.io.InputStream;
import java.util.List;import com.itheima.mapper.UserMapper;
import com.itheima.pojo.User;
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 MyBatisDemo {public static void main(String[] args) throws IOException {//加载mybatis核心配置文件,获取SqlSessionFactoryString resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);// 获取SqlSession对象SqlSession sqlSession = sqlSessionFactory.openSession();//获取UserMapper对象,用它直接执行sql方法UserMapper userMapper = sqlSession.getMapper(UserMapper.class);List<User>users = userMapper.selectAll();System.out.println(users);//释放资源sqlSession.close();}}
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><environments default="development"><environment id="development"><transactionManager type="JDBC"></transactionManager><dataSource type="POOLED"><!-- 数据库连接信息 --><property name="driver" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql:///mybatis?useSSL=false"></property><property name="username" value="root"></property><property name="password" value="1234"></property></dataSource></environment></environments><mappers>
<!-- <mapper class="com.itheima.mapper.UserMapper"></mapper>--><package name="com.itheima.mapper"/></mappers>
</configuration>
其中可以使用class=全类名,来找到mapper接口。
<mapper class="com.itheima.mapper.UserMapper"></mapper>
也可以使用包扫描的方式,来找到mapper接口。
<package name="com.itheima.mapper"/>
mybatis-config.xml(设置别名+多环境配置+顺序)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><!--设置别名,这样任意xml中出现限定名的地方都不需要加上前面的报名前缀。且类名不区分大小写--><!--譬如这里配置了后UserMapper中的代码就可以改为<select id = "selectAll" resultType ="User"> 或者<select id = "selectAll" resultType ="user">--><typeAliases><package name="com.itheima.pojo"/></typeAliases><!-- environments default属性可以切换不同的environment --><environments default="development"><environment id="development"><transactionManager type="JDBC"></transactionManager><dataSource type="POOLED"><!-- 数据库连接信息 --><property name="driver" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql:///mybatis?useSSL=false"></property><property name="username" value="root"></property><property name="password" value="1234"></property></dataSource></environment><environment id="learn"><transactionManager type="JDBC"></transactionManager><dataSource type="POOLED"><!-- 数据库连接信息 --><property name="driver" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql:///mybatis?useSSL=false"></property><property name="username" value="root"></property><property name="password" value="1234"></property></dataSource></environment></environments><mappers><mapper resource="com/itheima/mapper/UserMapper.xml"></mapper><package name="com.itheima.mapper"/></mappers></configuration>
mybatis-config.xml里面的属性是有顺序的,顺序如下:
- properties(属性)
- settings(设置)
- typeAliases(类型别名)
- typeHandler(类型处理器)
- objectFactory(对象工厂)
- plugins(插件)
- environments(环境配置)
- transactionManager(事务管理器)
- dataSource(数据源)
- mappers(映射器)
3.0代码 注解开发
流程
和2.0 代理开发一样,但是xml文件里面不需要再写select属性,只需要在pojo的类上面加上对应的注解即可。
UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.itheima.mapper.UserMapper"></mapper>
UserMapper.java
package com.itheima.mapper;import com.itheima.pojo.User;
import org.apache.ibatis.annotations.Select;import java.util.List;public interface UserMapper {@Select("select * from tb_user")List<User> selectAll();
}