<environments default="id2">
<environment id="id1">
事务类型
数据源
environment>
<environment id="id2">
environment>
<environment id="id3">
environment>
environments>
每个数据库环境可以指定不同的事务类型、数据源配置
——因此不同的数据库环境,可以连接不同的数据库。
事务管理器属于数据库环境的配置,因此它放在
通过
该属性值应该指定为事务管理器工厂的实现类的完整类名
主类
package lee;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
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.itcheng.app.dao.NewsMapper;
import org.itcheng.app.domain.News;
public class NewsManager
{
// SqlSessionFactory应该是应用级别
private static SqlSessionFactory sqlSessionFactory;
public static void main(String[] args) throws IOException
{
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
// 1. 创建SqlSessionFactory
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 2. 打开SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 保存消息
// saveNews(sqlSession);
// 更新消息
// updateNews(sqlSession);
// 删除消息
// deleteNews(sqlSession);
// 查询消息
selectNews(sqlSession);
}
public static void saveNews(SqlSession sqlSession)
{
News news = new News(null, "测试标题", "测试内容");
NewsMapper newsMapper = sqlSession.getMapper(NewsMapper.class);
newsMapper.saveNews(news);
sqlSession.commit();
sqlSession.close();
}
public static void updateNews(SqlSession sqlSession)
{
News news = new News(1, "修改的标题", "修改的内容");
NewsMapper newsMapper = sqlSession.getMapper(NewsMapper.class);
newsMapper.updateNews(news);
sqlSession.commit();
sqlSession.close();
}
public static void deleteNews(SqlSession sqlSession)
{
NewsMapper newsMapper = sqlSession.getMapper(NewsMapper.class);
newsMapper.deleteNews(2);
sqlSession.commit();
sqlSession.close();
}
public static void selectNews(SqlSession sqlSession)
{
// 此处的NewsMapper只是一个接口
// MyBatis会使用JDK的动态代理为Mapper接口生成实现类
NewsMapper newsMapper = sqlSession.getMapper(NewsMapper.class);
System.out.println(newsMapper.getClass());
List<?> list = newsMapper.findNews(-1);
System.out.println(list);
sqlSession.commit();
sqlSession.close();
}
}
接口类
package org.itcheng.app.dao;
import java.util.List;
import org.itcheng.app.domain.News;
// Mapper组件相当于DAO组件
public interface NewsMapper
{
int saveNews(News news);
int updateNews(News news);
void deleteNews(Integer id);
List<News> findNews(Integer id);
}
mapper配置文件
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.itcheng.app.dao.NewsMapper">
<insert id="saveNews">
insert into news_inf values (null, #{title}, #{content})
insert>
<update id="updateNews">
update news_inf set news_title = #{title}, news_content = #{content}
where news_id=#{id}
update>
<delete id="deleteNews">
delete from news_inf where news_id = #{xyz}
delete>
<select id="findNews" resultType="news">
select news_id id, news_title title, news_content content from news_inf where news_id > #{id}
select>
mapper>
用户类
package org.itcheng.app.domain;
import org.apache.ibatis.transaction.jdbc.JdbcTransaction;
public class News
{
private Integer id;
private String title;
private String content;
//set和get方法 有参无参构造器
}
package org.itcheng.app.mybatis;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.apache.ibatis.session.TransactionIsolationLevel;
import org.apache.ibatis.transaction.Transaction;
/**
* 可参照JdbcTransaction进行编写
*/
public class FkTransaction implements Transaction
{
// timeout该属性用于配置超时时长
private Integer timeout;
// 数据库连接
private Connection connection;
protected DataSource dataSource;
// 隔离级别
protected TransactionIsolationLevel level;
protected boolean autoCommit;
public FkTransaction(Integer timeout, DataSource ds,
TransactionIsolationLevel desiredLevel, boolean desiredAutoCommit)
{
this.timeout = timeout;
dataSource = ds;
level = desiredLevel;
autoCommit = desiredAutoCommit;
}
public FkTransaction(Integer timeout, Connection connection)
{
this.timeout = timeout;
this.connection = connection;
}
/**
* 获取数据库连接
*/
@Override
public Connection getConnection() throws SQLException
{
if (connection == null)
{ //打开连接
openConnection();
}
return connection;
}
/**
* 提交事务
*/
@Override
public void commit() throws SQLException
{
if (connection != null && !connection.getAutoCommit())
{
connection.commit();
}
}
/**
* 回滚事务
*/
@Override
public void rollback() throws SQLException
{
if (connection != null && !connection.getAutoCommit())
{
connection.rollback();
}
}
/**
* 关闭数据库连接
*/
@Override
public void close() throws SQLException
{
if (connection != null)
{
connection.setAutoCommit(true);
connection.close();
}
}
/**
* 指定超时级别
*/
@Override
public Integer getTimeout() throws SQLException
{
return this.timeout;
}
/**
* 打开连接
*/
protected void openConnection() throws SQLException
{
connection = dataSource.getConnection();
if (level != null)
{ //设置隔离级别
connection.setTransactionIsolation(level.getLevel());
}
// 设置自动提交方式
connection.setAutoCommit(autoCommit);
}
}
package org.itcheng.app.mybatis;
import java.sql.Connection;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.ibatis.session.TransactionIsolationLevel;
import org.apache.ibatis.transaction.Transaction;
import org.apache.ibatis.transaction.TransactionFactory;
/**
* 可参照JdbcTransactionFactory进行编写
*/
public class FkTransactionFactory implements TransactionFactory
{
private Integer timeout;
@Override
public void setProperties(Properties props)
{
// 將配置事务管理器时所有属性都以Properties对象传入
this.timeout = Integer.parseInt(props.getProperty("timeout"));
}
@Override
public Transaction newTransaction(Connection conn)
{
return new FkTransaction(timeout, conn);
}
@Override
public Transaction newTransaction(DataSource dataSource,
TransactionIsolationLevel level, boolean autoCommit)
{
// TODO Auto-generated method stub
return new FkTransaction(timeout, dataSource, level, autoCommit);
}
}
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias type="org.itcheng.app.mybatis.FkTransactionFactory" alias="FK"/>
<package name="org.itcheng.app.domain"/>
typeAliases>
<environments default="mysql">
<environment id="mysql">
<transactionManager type="FK" >
<property name="timeout" value="5"/>
transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC" />
<property name="username" value="root" />
<property name="password" value="root" />
dataSource>
environment>
environments>
<mappers>
<mapper resource="org/itcheng/app/dao/NewsMapper.xml" />
mappers>
configuration>