在config包下面创建一个JdbcConfig类实现数据库的连接
public class JdbcConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean
public DataSource dataSource(){
DruidDataSource ds=new DruidDataSource();
ds.setDriverClassName(driver);
ds.setUrl(url);
ds.setUsername(username);
ds.setPassword(password);
return ds;
}
}
再创建一个MybatisConfig类配置相关的信息
public class MybatisConfig {
@Bean
public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
SqlSessionFactoryBean ssfb=new SqlSessionFactoryBean();
ssfb.setTypeAliasesPackage("com.pojo");
ssfb.setDataSource(dataSource);
return ssfb;
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
MapperScannerConfigurer msc=new MapperScannerConfigurer();
msc.setBasePackage("com.dao");
return msc;
}
}
pom.xml里面的依赖导入
org.springframework
spring-context
5.2.10.RELEASE
org.apache.struts
struts-tiles
1.3.8
com.alibaba
druid
1.1.16
org.mybatis
mybatis
3.5.6
mysql
mysql-connector-java
5.1.47
org.springframework
spring-jdbc
5.2.10.RELEASE
org.mybatis
mybatis-spring
1.3.0
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)