Spring框架 -------mybatis优化

流程 :

   第一步:在xml中将spring属性信息配置进去,并设置一个监听器帮助spring属性信息加载进去

   

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instanc
e"
xsi:schemaLocation="http://java.sun.com/xml/ns/java
ee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<!-- 上下文参数 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- spring 配置文件 -->
<param-value>classpath:applicationContext.xml</para
m-value>
</context-param>
<!-- 封装了一个监听器,帮助加载 Spring 的配置文件爱 -->
<listener>
<listener-class>org.springframework.web.context.Con
textLoaderListener</listener-class>
</listener>
</web-app>

配置文件 applicationContext.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/sc
hema/beans
http://www.springframework.org/schema/beans/spring-be
ans.xsd">
<!-- 数据源封装类 .数据源:获取数据库连
接,spring-jdbc.jar 中-->
<bean id="dataSouce"class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"value="com.mysql.jdbc.Driver"></property>

<property name="url"value="jdbc:mysql://localhost:3306/ssm"></property>

<property name="username"value="root"></property>

<property name="password"value="smallming"></property>

</bean>
<!-- 创建 SqlSessionFactory 对象 -->
<bean id="factory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接信息来源于 dataSource -->

<property name="dataSource"ref="dataSouce"></property>
</bean>

<!-- 扫描器相当于 mybatis.xml 中 mappers 下 package 标
签,扫描 com.bjsxt.mapper 包后会给对应接口创建对象-->
<bean
class="org.mybatis.spring.mapper.MapperScannerConfigu
rer">
<!-- 要扫描哪个包 -->
<property name="basePackage"value="com.bjsxt.mapper"></property>
<!-- 和 factory 产生关系 -->
<property name="sqlSessionFactory"ref="factory"></property>
</bean>
<!-- 由 spring 管理 service 实现类 -->
<bean id="airportService"class="com.bjsxt.service.impl.AirportServiceImpl">

<property name="airportMapper"ref="airportMapper"></property>
</bean>
</beans>

4. 编写代码


4.1 正常编写 pojo


4.2 编写 mapper 包下时必须使用接口绑定方案或注解方案(必须
有接口)
4.3 正常编写 Service 接口和 Service 实现类


3.3.1 需要在 Service 实现类中声明 Mapper 接口对象,并生成
get/set 方法  然后就实现了mapper对象的注入 


4.4 spring 无法管理 Servlet,在 service 中取出 Servie 对象  在Init 函数中取出service对象,然后在service方法中使用并返回结果

 

@WebServlet("/airport")
public class AirportServlet extends HttpServlet{
private AirportService airportService;
@Override
public void init() throws ServletException {
//对 service 实例化
// ApplicationContext ac = new
ClassPathXmlApplicationContext("applicationContext.xml");
//spring 和 web 整合后所有信息都存放在

webApplicationContext ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
        
airportService=ac.getBean("airportService",AirportServiceImpl.class);

}
@Override
protected void service(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException {
req.setAttribute("list", airportService.show());
req.getRequestDispatcher("index.jsp").forward(req,resp);

}
}

猜你喜欢

转载自blog.csdn.net/weixin_41298572/article/details/88901852