我的笔记 SSM直接写Mapper接口的bean整合
来源:互联网 发布:南昌市残疾淘宝培训 编辑:程序博客网 时间:2024/06/08 11:42
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>
@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());
}
} 0 0
- 我的笔记 SSM直接写Mapper接口的bean整合
- SSM整合单表的mapper映射文件
- mybatis整合ss的时候,无法autowire使用mapper的自动注入,找不到bean:NoSuchBeanDefinitionException
- ssm的整合
- ssm的初步整合
- ssm的简单整合
- ssm整合的配置文件
- SSM框架的整合
- SSM框架的整合
- SSM框架的整合
- SSM的整合
- ssm整合的配置文件
- ssm框架的整合
- SSM框架的整合
- SSM框架的整合
- SSM整合的配置
- ssm框架的整合
- SSM整合的配置
- 【面试题】和为S的两个数、和为S的连续正数序列
- Struts中解耦与耦合
- Android原生分享功能的思考与实现
- 抓取网页上的邮箱信息
- 正则表达式相关整合
- 我的笔记 SSM直接写Mapper接口的bean整合
- zoj 3949 Edge to the Root 2017浙大校赛B
- 制造与管理科学数据
- 数据表
- hdu 1254 推箱子
- Algorithm Gossip(14) 蒙地卡罗法求 PI
- POJ A Bug's Life POJ 2492(和食物链差不多的一道题)
- spring的原理
- Huffuman编码与译码C语言实现