主(父)配置类的编写
// 声明这是一个配置类
@Configuration
// 配置哪个包下的类需要加载到IOC容器中
@ComponentScan("com.xx.dao")
// 需要导入的配置类有哪些
@Import(value = {
SqlSessionFactoryBuilderConfig.class , DriverManger_Dome.class})
// 当需要导入配置文件时 classpath:;类路径
@PropertySources(value = {
@PropertySource("classpath:jdbcConfig.properties")})
public class SpringConfiguration {
}
1 > DriverMange配置类,用与演示如何进行值的注入,及其对应的PropertySources注解的使用方法
<- 以下为jdbcConfig.properties文件中的内容 ->
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://159.75.xx.xxx:3306/dome02?characterEncoding=utf8
jdbc.username=root
jdbc.password=密码就不展示了。。上次展示万。服务器都挂了。。
public class DriverManger_Dome {
// 与主配置类中 PropertySources注解相关,采用EL表达式进行取值
@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(name = "conn") // 为当前方法返回的类设置一个ID值,并将其放入IOC容器,方便取用
public Connection getConn() {
Connection connection = null;
try {
Class.forName(driver);
connection = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
}
1.1 > 测试类测试
getBean方法:通过存入IOC容器的ID,取用对应的类
@Test
public void method_02() {
// 从容器中将相关配置取出,并将需要的类全部放入容器
ApplicationContext app = new AnnotationConfigApplicationContext(SpringConfiguration.class);
// 获取id对应的类
Connection conn = app.getBean("conn", Connection.class);
// 编写Sql语句
String select = "select * from account";
// 获取执行对象
PreparedStatement preparedStatement = null;
// 设置结果对象
ResultSet resultSet = null;
try {
// 执行Sql语句
preparedStatement = conn.prepareStatement(select);
// 将结果集封装为结果对象
resultSet = preparedStatement.executeQuery();
// 编写提示信息
System.out.println("id\tname\tmoney");
// 遍历结果集
while (resultSet.next()){
String name = null;
// 有点小强迫症,为了结果的对齐
if (resultSet.getString("name") != null) {
String temp = resultSet.getString("name");
name = temp.length() > 3 ? temp : temp + "\t";
}
// 显示每一条数据
System.out.println(resultSet.getInt("id") + "\t" + name + "\t" + resultSet.getFloat("money"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
1.2 > dome02库account表
1.3 > DriverManger测试结果
2 > MyBatis的配置
public class SqlSessionFactoryBuilderConfig {
// 为返回的对象设置一个id
@Bean(name = "sqlSessionFactory")
// 为形参注入指定的值inputStream1
public SqlSessionFactory sqlSession(@Qualifier("inputStream1") InputStream inputStream){
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
return builder.build(inputStream);
}
@Bean(name = "inputStream")
public InputStream getFileInputStream(){
InputStream resourceAsStream = null;
try {
// 获取配置文件
resourceAsStream = Resources.getResourceAsStream("./MybatisConfigSqlMap.xml");
} catch (IOException e) {
e.printStackTrace();
}
return resourceAsStream;
}
@Bean(name = "inputStream1")
public InputStream getFileInputStream1(){
InputStream resourceAsStream = null;
try {
resourceAsStream = Resources.getResourceAsStream("./MybatisConfigSqlMap.xml");
} catch (IOException e) {
e.printStackTrace();
}
return resourceAsStream;
}
}
2.1 > 测试
@Test
public void method_01() {
// 获取配置类,巴拉巴拉
ApplicationContext app = new AnnotationConfigApplicationContext(SpringConfiguration.class);
// 获取SQLSessionFactory对象
SqlSessionFactory sqlSessionFactory = app.getBean("sqlSessionFactory", SqlSessionFactory.class);
// 获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
// 获取AccountDao的实现类,实际上是mybatis棒我们实现了
AccountDao mapper = sqlSession.getMapper(AccountDao.class);
// 使用AccountDao中的方法findAll
mapper.findAll().forEach(System.out::println);
}