如何将Java代码配置MyBatis实现长尾词查询功能?
- 内容介绍
- 文章标签
- 相关推荐
本文共计403个文字,预计阅读时间需要2分钟。
java替换mybatis-config.xml,该方法返回SqlSessionFactory,我们就可以获取SqlSession了。
+SqlSessionFactory+mybatisConfig(String driver, String url, String username, String password) throws SQLException { // 获取DataSource DataSource dataSource=createDataSource(driver, url, username, password); // 初始化SqlSessionFactory SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(dataSource); return sqlSessionFactory;}
SqlSessionFactory mybatisConfig(String driver, String url, String username,
String password) throws SQLException {
// 获取DataSource,DataSource接口有多个实现类,我们用的是mybatis给我们提供的PooledDataSource
PooledDataSource pooledDataSource = new PooledDataSource(driver, url,
username, password);
// 若需要配置pooledDatasource,则可以用他的set方法,如
pooledDataSource.setLoginTimeout(6000);
DataSource dataSource = pooledDataSource;
// 配置事务管理,这里我们使用JDBC的事务
TransactionFactory trcFactory = new JdbcTransactionFactory();
// 配置Environment对象,"development"是我们给起的名字
Environment env = new Environment("development", trcFactory, dataSource);
// 创建Configuration对象
Configuration config = new Configuration(env);
//
PooledDataSource pooledDataSource = new PooledDataSource(driver, url,username, password);
对应
TransactionFactory trcFactory = new JdbcTransactionFactory(); 对应 解析3:配置Environment
Environment env = new Environment("development", trcFactory, dataSource);
对应
Configuration config = new Configuration(env);
对应
TypeAliasRegistry aliases = config.getTypeAliasRegistry();
aliases.registerAlias("bean", TestBean.class);
对应
config.addMapper(DaoMapper.class);
对应
本文共计403个文字,预计阅读时间需要2分钟。
java替换mybatis-config.xml,该方法返回SqlSessionFactory,我们就可以获取SqlSession了。
+SqlSessionFactory+mybatisConfig(String driver, String url, String username, String password) throws SQLException { // 获取DataSource DataSource dataSource=createDataSource(driver, url, username, password); // 初始化SqlSessionFactory SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(dataSource); return sqlSessionFactory;}
SqlSessionFactory mybatisConfig(String driver, String url, String username,
String password) throws SQLException {
// 获取DataSource,DataSource接口有多个实现类,我们用的是mybatis给我们提供的PooledDataSource
PooledDataSource pooledDataSource = new PooledDataSource(driver, url,
username, password);
// 若需要配置pooledDatasource,则可以用他的set方法,如
pooledDataSource.setLoginTimeout(6000);
DataSource dataSource = pooledDataSource;
// 配置事务管理,这里我们使用JDBC的事务
TransactionFactory trcFactory = new JdbcTransactionFactory();
// 配置Environment对象,"development"是我们给起的名字
Environment env = new Environment("development", trcFactory, dataSource);
// 创建Configuration对象
Configuration config = new Configuration(env);
//
PooledDataSource pooledDataSource = new PooledDataSource(driver, url,username, password);
对应
TransactionFactory trcFactory = new JdbcTransactionFactory(); 对应 解析3:配置Environment
Environment env = new Environment("development", trcFactory, dataSource);
对应
Configuration config = new Configuration(env);
对应
TypeAliasRegistry aliases = config.getTypeAliasRegistry();
aliases.registerAlias("bean", TestBean.class);
对应
config.addMapper(DaoMapper.class);
对应

