我的笔记 SSM直接写Mapper接口的bean整合

    xiaoxiao2021-04-14  79

    DeptMapping.xml映射文件 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper   PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"   "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.ssm.dao.DeptDao">     <select id="findAll" resultType="com.ssm.domain.Dept">         select deptno,dname,loc from dept     </select> </mapper> DeptMapper 类为接口 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" xmlns:context="http://www.springframework.org/schema/context"     xmlns:myns="http://www.mycompany.com/schema/myns" xmlns:jdbc="http://www.springframework.org/schema/jdbc"     xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"     xsi:schemaLocation="         http://www.springframework.org/schema/beans          http://www.springframework.org/schema/beans/spring-beans-3.2.xsd         http://www.mycompany.com/schema/myns http://www.mycompany.com/schema/myns/myns-3.2.xsd         http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context-3.2.xsd         http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd         http://www.springframework.org/schema/jee         http://www.springframework.org/schema/jee/spring-jee-3.2.xsd         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd         http://www.springframework.org/schema/mvc         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">     <context:component-scan base-package="com.ssm" />     <mvc:annotation-driven />     <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"         destroy-method="close">         <property name="driverClassName" value="oracle.jdbc.OracleDriver"></property>         <property name="url" value="jdbc:oracle:thin:@localhost:1522:MYORA"></property>         <property name="username" value="scott"></property>         <property name="password" value="123"></property>     </bean>          <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">         <property name="dataSource" ref="myDataSource"/>         <property name="mapperLocations" value="classpath:com/ssm/domain/*.xml"/>     </bean>          <!-- 定义Mapper -->     <bean id="deptMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">         <property name="mapperInterface" value="com.ssm.dao.DeptMapper"/>         <property name="sqlSessionFactory" ref="sqlSessionFactory"/>     </bean>   </beans> mybatis.xml mybatis框架的配置文件 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration   PUBLIC "-//mybatis.org//DTD Config 3.0//EN"   "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> 导入文件     <properties resource="jdbc.properties"></properties> 简写配置     <typeAliases>     <!--          <typeAlias type="com.eduask.mbt.domain.Student" alias="haha"></typeAlias>         <typeAlias type="com.eduask.mbt.domain.Teacher" alias="tea"></typeAlias>          -->          <package name="com.eduask.mbt.domain"/>               </typeAliases>     <!--       <environments default="development">         <environment id="development">             <transactionManager type="JDBC" />             <dataSource type="POOLED">                 <property name="driver" value="${driverClass}" />                 <property name="url" value="${jdbcUrl}" />                 <property name="username" value="${user}" />                 <property name="password" value="${password}" />             </dataSource>         </environment>     </environments>     -->     <mappers>         <mapper resource="com/ssm/domain/DeptMapping.xml" />     </mappers> </configuration> springmvc-servlet.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"     xmlns:context="http://www.springframework.org/schema/context"     xmlns:aop="http://www.springframework.org/schema/aop"     xmlns:tx="http://www.springframework.org/schema/tx"     xmlns:mvc="http://www.springframework.org/schema/mvc"     xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">     <!-- 视图解析器 -->     <bean id="jspViewReslover" class="org.springframework.web.servlet.view.InternalResourceViewResolver">         <property name="prefix" value="/WEB-INF/jsp/"></property>         <property name="suffix" value=".jsp"></property>     </bean> </beans> web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">   <display-name>0714springmvctest</display-name>   <welcome-file-list>     <welcome-file>index.html</welcome-file>     <welcome-file>index.htm</welcome-file>     <welcome-file>index.jsp</welcome-file>     <welcome-file>default.html</welcome-file>     <welcome-file>default.htm</welcome-file>     <welcome-file>default.jsp</welcome-file>   </welcome-file-list>        <!-- 集成Spring -->     <listener>         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>     </listener>     <context-param>         <param-name>contextConfigLocation</param-name>         <param-value>classpath:applicationContext.xml</param-value>     </context-param>         <!-- 配置spring的前端控制器 -->   <servlet>       <servlet-name>springmvc</servlet-name>       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>   </servlet>      <servlet-mapping>       <servlet-name>springmvc</servlet-name>       <url-pattern>*.do</url-pattern>   </servlet-mapping>         <!-- spring处理中文乱码的过滤器 -->   <filter>       <filter-name>chinese</filter-name>       <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>     <init-param>           <param-name>encoding</param-name>           <param-value>utf-8</param-value>       </init-param>   </filter>      <filter-mapping>             <filter-name>chinese</filter-name>             <url-pattern>*.do</url-pattern>   </filter-mapping>    </web-app>       @Test     public void testFindAll() throws IOException{         String conf="applicationContext.xml";         ApplicationContext ac=new ClassPathXmlApplicationContext(conf);         DeptMapper mapper=ac.getBean("deptMapper",DeptMapper.class);         List<Dept> list=mapper.findAll();         for(Dept dept:list){             System.out.println(dept.getDeptno()+dept.getDname()+dept.getLoc());         }     }

    转载请注明原文地址: https://ju.6miu.com/read-670333.html

    最新回复(0)