Spring整合MyBatis

1. 导入jar包

     导入mybatis所有jarspring基本包;

     spring-jdbc   spring-tx(事务)    spring-aop(面向切面)    spring-web(配置监听器Listener)     spring整合mybatis的包等

          

2. 先配置web.xml

web.xml的配置中<context-param>配置作用
1. 启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件web.xml.读两个节点: <listener></listener> 和 <context-param></context-param>
2.紧接着,容器创建一个ServletContext(上下文),这个WEB项目所有部分都将共享这个上下文.
3.容器将<context-param></context-param>转化为键值对,并交给ServletContext.
4.容器创建<listener></listener>中的类实例,即创建监听.
5.在监听中会有contextInitialized(ServletContextEvent args)初始化方法,在这个方法中获得ServletContext = ServletContextEvent.getServletContext();
context-param的值 = ServletContext.getInitParameter("context-param的键");

<?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-instance"

                                       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee                       

                                                                          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

     <!--  加载Spring的配置文件 -->

           <!-- 上下文参数 -->

          <context-param>

                    <param-name>contextConfigLocation</param-name>

                    <!-- spring配置文件 -->

                   <param-value>classpath:applicationContext.xml</param-value>

           </context-param>

          <!-- 封装了一个监听器listener,帮助加载Spring的配置文件 -->

         <listener>

                   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

         </listener>

</web-app>

3.编写spring配置文件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/schema/beans

                                               http://www.springframework.org/schema/beans/spring-beans.xsd">

      <!-- 数据源封装类 .数据源:获取数据库连接,spring-jdbc.jar中-->

      <bean id="dataScource 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="lc817"></property>

      </bean>

      <!-- 创建SqlSessionFactory对象 -->

      <bean id="factory class="org.mybatis.spring.SqlSessionFactoryBean">

               <!-- 数据库连接信息来源于dataSource -->

               <property name="dataSource"  ref="dataScource"></property>

      </bean>

      <!-- 扫描包:扫描器相当于mybatis.xml中 mappers下package标签,扫描com.bjsxt.mapper包后会给对应接口创建对象-->

      <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

              <!-- 要扫描哪个包 -->

              <property name="basePackage"  value="com.mzx.mapper"></property>

              <!-- 和factory产生关系 -->

              <property name="sqlSessionFactory"  ref="factory"></property>

      </bean>

      <!-- 由spring管理service实现类,配置ServiceImp -->

      <bean id="studentServiceImp"  class="com.mzx.serviceImp.StudentServiceImp">

              <property name="sm"  ref="studentMapper"></property>

      </bean>

</beans>

4.编写代码

          1. 正常编写pojo

          2. 编写mapper包下时,必须使用接口绑定方案或注解方案(必须有接口)

          3. 正常编写Service接口和Service实现类

                3.3.1 需要在Service实现类中声明Mapper接口对象,并生成get/set方法

public  interface  StudentService {

      List<Student>  getAllStudentInfo();

}

public class StudentServiceImp implements StudentService {

         private StudentMapper sm;

         public StudentMapper getSm() { 

                 return sm;

                   }

         public void setSm(StudentMapper sm) {

                this.sm = sm;

                   }

        @Override 

        public List<Student> getAllStudentInfo() {

               return sm.selStu();

}

          4. spring无法管理Servlet,service中取出Service对象

@WebServlet("/show")

public class StudentServlet extends HttpServlet{  

         private  StudentService  ss;

        @Override  

        public void init() throws ServletException {

              /*  //获取Spring容器对象

              ApplicationContext ac=new ClassPathXmlApplicationContext("applicationcontext.xml");

              //获取响应对象

              ss=(StudentService) ac.getBean("serviceImp");  */


        //spring和web整合后所有信息都存放在webApplicationContext 

                  ApplicationContext ac=WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());

                   ss=(StudentService) ac.getBean("serviceImp");}

        @Override

        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

                 //设置请求编码格式

                 //设置响应编码格式

                 //获取请求信息

                 //处理请求信息

                        List<Student> list=ss.getAllStudentInfo();

                 //响应处理结果

                        System.out.println(list);

                      }}

木子璇总结时刻:欢迎小伙伴们提出建议哦,如有错误,望大神指出哦,谢谢啦。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值