Spring整合mybatis

1.  Spring整合mybatis

1、 导入jar包mybatis+spring+mybatis_spring等jar包。

2、 创建配置文件web.xml,spring.xml配置文件

a)    Web.xml

<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>

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

         </context-param>

         <!-- spring-web包中 -->

         <listener>

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

         </listener>

</web-app>

b)    Spring.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:aop="http://www.springframework.org/schema/aop" 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

        http://www.springframework.org/schema/aop

        http://www.springframework.org/schema/aop/spring-aop.xsd

        " default-autowire="byName">

       <!-- mybatis的数据源配置信息,由spring 来完成! -->

       <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

              <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>

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

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

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

       </bean>

 

       <!-- 配置sqlSessionFactory mybatis测试类中的SqlSessionFactory -->

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

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

       </bean>

 

       <!-- 配置myatismapper文件  sqlSession.getMapper(class<T> class) -->

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

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

              <property name="sqlSessionFactoryBeanName" value="factory"></property>

       </bean>

       <bean id="userService" class="com.bjsxt.service.impl.UserServiceImpl">

       </bean>

      

       <!-- 第二种方式扫描mapper接口 指定到具体的mapper getSqlSession().getMapper(UserMapper.class)-->

       <!-- <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">

              <property name="mapperInterface" value="com.bjsxt.mapper.UserMapper"></property>

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

       </bean>

      

       创建UserServiceImpl 对象为 userMapper 添加实现类

       <bean id="userService" class="com.bjsxt.service.impl.UserServiceImpl">

               <property name="userMapper" ref="userMapper"></property>

       </bean> -->

</beans>

3、 创建完整的项目目录结构

4、

5、 Service实现类

public class UserServiceImpl implements UserService {

       private UserMapper userMapper;

       public void setUserMapper(UserMapper userMapper) {

              this.userMapper = userMapper;

       }

       @Override

       public List<User> findAll() {

              return userMapper.findAll();

       }

}

6、 Servlet内容

@WebServlet("/UserList")

public class UserList extends HttpServlet {

       private UserService userService;

       @Override

       public void init() throws ServletException {

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

              userService = ac.getBean("userService",UserService.class);

       }

       @Override

       protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

              List<User> list = userService.findAll();

              if (list.size()>0) {

                     request.getSession().setAttribute("list", list);

                     request.getRequestDispatcher("list.jsp").forward(request, response);

              }

       }

}

7、 List.jsp

<body>

       <table>

              <tr>

                     <td>id</td>

                     <td>name</td>

                     <td>pwd</td>

              </tr>

              <c:forEach items="${list }" var="info">

                     <tr>

                            <td>${info.id }</td>

                            <td>${info.username }</td>

                            <td>${info.pwd }</td>

                     </tr>

              </c:forEach>

       </table>

</body>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值