两种⽅式(xml+代码)构建SqlSessionFactory+完整实现⾸先创建类、接⼝、数据库:
entity包下Admin类:
package com.ity;
public class Admin {
private int aId;
private String aAccount;
private String aPassword;
private String aRank;
public Admin(int aId, String aAccount, String aPassword, String aRank) {
this.aId = aId;
this.aAccount = aAccount;
this.aPassword = aPassword;
this.aRank = aRank;
}
public int getaId() {
return aId;
}
public void taId(int aId) {
this.aId = aId;
}
public String getaAccount() {
return aAccount;
}
public void taAccount(String aAccount) {
this.aAccount = aAccount;
}
public String getaPassword() {
return aPassword;
}
public void taPassword(String aPassword) {
this.aPassword = aPassword;
}
public String getaRank() {
return aRank;
}
public void taRank(String aRank) {
this.aRank = aRank;
}
@Override
public String toString() {
return "Admin{" +
"aId=" + aId +
", aAccount='" + aAccount + '\'' +
", aPassword='" + aPassword + '\'' +
", aRank='" + aRank + '\'' +
'}';
}
}
View Code
dao包下的AdminDao接⼝
public interface AdminDao {
List<Admin> listAll();
Admin getById(int aId);
}
View Code
rvice包下AdminService
public interface AdminService {
List<Admin> listAll();
Admin getById(int aId);
}
View Code
impl包下AdminServiceImpl
public class AdminServiceImpl implements AdminService {
@Override
public List<Admin> listAll() {
return null;
}
@Override
public Admin getById(int aId) {
return null;
}
}
View Code
⼀、使⽤xml构建SqlSessionFactory
<!-- 数据库连接池 -->
<bean id="dataSource" class="hange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="org.mariadb.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mariadb://localhost:3306/wbg_logistics"/> <property name="ur" value="root"/>
<property name="password" value="123456"/>
<!-- c3p0连接池的私有属性 -->
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="10"/>
<!-- 关闭连接后不⾃动commit -->
<property name="autoCommitOnClo" value="fal"/>
<!-- 获取连接超时时间 -->
<property name="checkoutTimeout" value="10000"/>
<!-- 当获取连接失败重试次数 -->
<property name="acquireRetryAttempts" value="2"/>
</bean>
<!-- 配置SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="batis.spring.SqlSessionFactoryBean"> <!-- 注⼊数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置MyBaties全局配置⽂件:l -->
<property name="configLocation" value="l"/>
<!-- 扫描entity包使⽤别名 -->
<property name="typeAliasPackage" value="com.wbg.springJavaConfig.dao"/> <!-- 扫描sql配置⽂件:mapper需要的xml⽂件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
View Code
⼆、使⽤代码构建SqlSessionFactory
第⼀步:配置DataSource
有三种⽅式,推荐使⽤C3p0
/**
* C3p0的连接池
* @return
* @throws PropertyVetoException
*/
@Bean
public DataSource getC3p0DataSource() throws PropertyVetoException {
ComboPooledDataSource dataSource=new ComboPooledDataSource();
dataSource.tDriverClass("org.mariadb.jdbc.Driver");
dataSource.tJdbcUrl("jdbc:mariadb://localhost:3306/wbg_logistics");
dataSource.tUr("root");
dataSource.tPassword("123456");
dataSource.tMaxPoolSize(30);
return dataSource;
}
/**
* MyBatis的连接池
* @return
*/
//@Bean
public DataSource getMyBatisDataSource(){
/
/数据库连接池
PooledDataSource dataSource = new PooledDataSource();
//设驱动
dataSource.tDriver("org.mariadb.jdbc.Driver");
//⽤户名
dataSource.tUrname("root");
//密码
dataSource.tPassword("123456");
//数据库连接
dataSource.tUrl("jdbc:mariadb://localhost:3306/wbg_logistics");
dataSource.tDefaultAutoCommit(fal);
return dataSource;
}
/**
* Spring⾃带的SimpleDriverDataSource
* @return
*/
//@Bean
public DataSource getSimpleDriverDataSource(){
SimpleDriverDataSource dataSource=new SimpleDriverDataSource();
return dataSource;
}
View Code
第⼆步:配置SqlSessionFactoryBean
需要xml配置的代码:
/**
* 获取SqlSessionFactoryBean
*
* @return
*/
@Bean
public SqlSessionFactoryBean getSqlSessionFactory(DataSource dataSource){
SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean();
//注⼊数据库连接池
sqlSessionFactoryBean.tDataSource(dataSource);
//配置MyBaties全局配置⽂件:l
sqlSessionFactoryBean.tConfigLocation(new ClassPathResource("l"));
//扫描entity包使⽤别名
sqlSessionFactoryBean.tTypeAliasPackage("com.wbg.springJavaConfig.dao");
//扫描sql配置⽂件:mapper需要的xml⽂件
sqlSessionFactoryBean.tMapperLocations(new Resource[]{new ClassPathResource("classpath:mapper/*.xml")});
return sqlSessionFactoryBean;
}
View Code
不需要xml的⽅式:
MySqlSessionFactory 类
package com.wbg.springJavaConfig.Mybatis;
hange.v2.c3p0.ComboPooledDataSource;
import com.wbg.springJavaConfig.dao.AdminDao;
import com.ity.Admin;
import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.ssion.*;
import org.ansaction.TransactionFactory;
import org.ansaction.jdbc.JdbcTransactionFactory;
import javax.sql.DataSource;
import java.beans.PropertyVetoException;
public class MySqlSessionFactory {
public static SqlSession getopenSession() throws PropertyVetoException {
SqlSessionFactory sqlSessionFactory=getSqlSessionFactory(getC3p0DataSource());
sqlSessionFactory.openSession();
SqlSession sqlSession=null;
try {
//打开SqlSession会话
sqlSession = sqlSessionFactory.openSession();
}catch (Exception ex){
}
return sqlSession;
}
public static DataSource getC3p0DataSource() throws PropertyVetoException {
ComboPooledDataSource dataSource=new ComboPooledDataSource();
dataSource.tDriverClass("org.mariadb.jdbc.Driver");
dataSource.tJdbcUrl("jdbc:mariadb://localhost:3306/wbg_logistics");
dataSource.tUr("root");
dataSource.tPassword("123456");
dataSource.tMaxPoolSize(30);
return dataSource;
}
public static SqlSessionFactory getSqlSessionFactory(DataSource dataSource){
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development", transactionFactory, dataSource);
//创建Configuration对象
org.apache.ibatis.ssion.Configuration configuration = new org.apache.ibatis.ssion.Configuration(environment);
//注册⼀个MyBatis上下⽂别名
//加⼊⼀个映射器
configuration.addMapper(AdminDao.class);
//使⽤SqlSessionFactoryBuilder构建SqlSessionFactory
//构建SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
return sqlSessionFactory;
}
}
View Code
进⾏使⽤:
1、修改AdminDao⾸先使⽤@Select注解
@Select("lect * from Admin")
List<Admin> listAll();
2、加⼊配置
SqlSession sqlSession = new MySqlSessionFactory().getopenSession(); public AdminServiceImpl() throws PropertyVetoException {
}
public List<Admin> listAll() {
AdminDao adminDao = Mapper(AdminDao.class);
return adminDao.listAll();
}
调⽤:
AdminServiceImpl adminService = new AdminServiceImpl();
List<Admin> list=adminService.listAll();
for (Admin admin : list) {
System.out.println(admin);
}
运⾏: