javaee框架整合

一:mvc

二:ssh

三:ssm

在spring整合mybatis中,mybatis需要做的事情:

1.创建一个和数据库对应的pojo,

查看我们数据库:

所以我们的pojo也要包括相同的字段:建立Items.java

package com.itheima.springmvc.pojo;

import java.util.Date;

public class Items {
    private Integer id;

    private String name;

    private Float price;

    private String pic;

    private Date createtime;

    private String detail;
//下边省略了setter and getter方法
...

2.配置对应的itemsMapper.xml用来写我们需要的sql语句:(这个语句只是针对这个数据库的操作),再加一个接口文件,这个接口文件就可以代替dao层的接口,这个接口中方法名和参数要和我们items.xml对应(感觉这个配置就是这个接口的实现)。

接口和配置如下:


public interface ItemsMapper {
String queryItemsById(int id);
}
<?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">
<!-- namespace:命名空间,用于隔离sql,还有一个很重要的作用,后面会讲 -->
<mapper namespace="test">

	<!-- id:statement的id 或者叫做sql的id-->
	<!-- parameterType:声明输入参数的类型 -->
	<!-- resultType:声明输出结果的类型,应该填写pojo的全路径 -->
	<!-- #{}:输入参数的占位符,相当于jdbc的? -->
	<select id="queryItemsById" parameterType="int"
		resultType="cn.itcast.mybatis.pojo.Items">
		SELECT * FROM `items` WHERE id  = #{id}
	</select>

</mapper>

3.mybatis框架主配置文件sqlMapConfig.xml

接口文件itemsMapper.java和配置文件items.xml放到同一个包下,在和spring整合的时候,我们的items.xml不是添加到SqlMapConfig.xml,而是添加到spring的xml配置中。

4.再spring中引入我们的mybatis中的所有xml配置,包括sqlMapperConfig.xml和(接口文件+itemsMapper.xml)所再的包

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">


	<context:property-placeholder location="classpath:db.properties"/>
	
	<!-- 数据库连接池 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="maxActive" value="10" />
		<property name="maxIdle" value="5" />
	</bean>
	
	<!-- Mybatis的工厂 -->
	<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"/>
		<!-- 核心配置文件的位置 -->
		<property name="configLocation" value="classpath:sqlMapConfig.xml"/>
	</bean>

	<!-- Mapper动态代理开发   扫描 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 基本包 ,这里面就是接口和配置所在的同一包-->
		<property name="basePackage" value="com.itheima.springmvc.dao"/>
	</bean>
	
	<!-- 注解事务 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	</bean>
	
	<!-- 开启注解 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
	
	
	
	
	
	
	
	

</beans>

未完,待续。。。。。。。。。。。。。。。

猜你喜欢

转载自blog.csdn.net/handsome2013/article/details/81148324