SqlSessionFactory创建(mybatis一)

时间:2024-04-09 09:00:27

SqlSessionFactory创建

mybatis的入口为SqlSessionFactory,创建方式大概如下

SqlSessionFactory创建(mybatis一)

基于xml方式

bean文件

public class Role {
	private int id;
	private String name;
	private String desc;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

}

mapper接口

public interface RoleMapper {
	Role selectRole(int id);

	void addRole(Role Role);

	void deleteRole(int id);

	void updateRole(Role Role);

	List<Role> selectAll();
}

mapper对应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="priv.dengjili.mapper.RoleMapper">

  <select id="selectAll" resultMap="ROLE">
    select * from Role
  </select>

  <select id="selectRole" resultType="Role" >
    select * from Role where id = #{id}
  </select>
  
  <insert id="addRole" parameterType="Role">
  	insert into Role(name) values(#{name})
  </insert>
  
  <update id="deleteRole" parameterType="int">
  	delete from Role where id = #{id}
  </update>
  
  <delete id="updateRole" parameterType="Role">
  	update Role set name = #{name} where id = #{id}
  </delete>
  
</mapper>

mybatis-configuration核心配置

<?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>
	
	<typeAliases>
		<typeAlias alias="Role" type="priv.dengjili.bean.Role"/>
	</typeAliases>

	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.cj.jdbc.Driver" />
				<property name="url" value="jdbc:mysql://127.0.0.1/test" />
				<property name="username" value="root" />
				<property name="password" value="root" />
			</dataSource>
		</environment>
	</environments>
	
	<mappers>
		<mapper resource="priv/dengjili/mapper/RoleMapper.xml" />
	</mappers>
</configuration>

xml创建方式测试

public class SqlSessionFactoryTest {

	public static void main(String[] args) throws IOException {
		String resource = "mybatis/mybatis-configuration.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		System.out.println(sqlSessionFactory);
	}

}

测试结果

DEBUG: Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
DEBUG: PooledDataSource forcefully closed/removed all connections.
DEBUG: PooledDataSource forcefully closed/removed all connections.
DEBUG: PooledDataSource forcefully closed/removed all connections.
DEBUG: PooledDataSource forcefully closed/removed all connections.
[email protected]9fb0

不使用xml方式

逻辑上与xml配置保持一致

public class SqlSessionFactoryNonXmlTest {

	public static void main(String[] args) throws IOException {
		// 数据库连接
		PooledDataSource dataSource = new PooledDataSource();
		dataSource.setDriver("com.mysql.cj.jdbc.Driver");
		dataSource.setUsername("root");
		dataSource.setPassword("root");
		dataSource.setUrl("jdbc:mysql://127.0.0.1/test");
		dataSource.setDefaultAutoCommit(false);
		// 事务
		TransactionFactory transactionFactory = new JdbcTransactionFactory();
		Environment environment = new Environment("development", transactionFactory, dataSource);
		// 创建configuration信息
		Configuration configuration = new Configuration(environment);
		// 别名设置
		configuration.getTypeAliasRegistry().registerAlias("Role", Role.class);
		// 添加一个映射器
		configuration.addMapper(RoleMapper.class);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
		System.out.println(sqlSessionFactory);
	}

}

建议

xml方式