JavaWeb学习之路——SSM框架之Spring(五)
前情提要请看JavaWeb学习之路——SSM框架之Spring(四)
整合Spring和Mybatis框架
1.在项目的lib下导入如下jar包
2.配置jdbc驱动
在jar包下找到对应的类文件,复制它的类名,下图为一个示例:
Spring xml文件配置数据库相关信息:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- id表示获取到对象标识
class 创建哪个类的对象
-->
<!-- 数据源封装类,相当于mybatis的environment标签,数据类在spring-jdbc包中 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/likui"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!-- 在mybatis-spring包中 -->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 扫描器,相当于mybatis下的mappers package标签 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.likui.mapper"></property>
<!-- 让factory与数据库相关联 -->
<property name="sqlSessionFactory" ref="factory"></property>
</bean>
</beans>
对应类:MapperScannerConfigurer->SqlSessionFactoryBean->DriverManagerDataSource,层层嵌套
3.使用流程
src下代码结构图:
1)在src下创建mapper包,里面是sql语句的使用方法
public interface FlowerMapper {
@Select("select * from flower")
List<Flower> selAll();
}
2)在src下创建pojo包,里面是类的构造方法和setter、getter方法,用来封装对象
public class Flower {
private int id;
private String name;
private double price;
private String production;
public void setList(List<Integer> list) {
this.list = list;
}
public Flower(int id, String name, double price, String production) {
this.id = id;
this.name = name;
this.price = price;
this.production = production;
}
public Flower() {}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getProduction() {
return production;
}
public void setProduction(String production) {
this.production = production;
}
3)创建service包,里面是工程项目的实现类,接口和它的实现类都放这里面
(1)接口
public interface FlowerService {
List<Flower> show() throws IOException ; //显示
}
(2)实现类
public class FlowerServiceImpl implements FlowerService{
private FlowerMapper flowermapper;
public FlowerMapper getFlowermapper() {
return flowermapper;
}
public void setFlowermapper(FlowerMapper flowermapper) {
this.flowermapper = flowermapper;
}
@Override
public List<Flower> show() throws IOException {
// TODO Auto-generated method stub
return flowermapper.selAll();
}
}
4)在Spring的beans.xml中创建类的对象
注意对应的首字母为大写的类或接口在引用时需要将首字母小写,接口不能创建bean,因为不能实例化
<bean id="flowerService" class="com.likui.service.FlowerServiceImpl">
<property name="flowermapper" ref="flowerMapper"></property>
</bean>
5)编写Test主函数来实现,在java文件中利用Spring IoC获取相应类对象
public class Test {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
//获取所有spring管理的bean的名称
//String []names=ac.getBeanDefinitionNames();
//for(String string:names) {
// System.out.println(string);
//}
FlowerServiceImpl flowerServiceImpl=ac.getBean("flowerService",FlowerServiceImpl.class);
List<Flower> list=flowerServiceImpl.show();
System.out.println("id\t\tname\t\tprice\t\tproduction");
for (Flower flower : list) {
System.out.println(flower.getId()+"\t\t"+flower.getName()+"\t\t"+
flower.getPrice()+"\t\t"+flower.getProduction());
}
}
}
6)实验结果展示: