Spring_Mybatis

spring_Mybatis的一般操作流程

1.创建pojo

2.sqlMapConfig.xml

3.applicationContext.xml

4.log4j.properties(日志,可有可无)

5.UserMapper接口和UserMapper.xml

1.创建pojo类

2.sqlMapConfig.xml

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

	<typeAliases>
		<package name="com.itheima.mybatis.pojo"/>
	</typeAliases>
	
	
	<mappers>
		<package name="com.itheima.mybatis.mapper"/>
	</mappers>
</configuration>

3.applicationContext.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">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/wzl" />
		<property name="username" value="root" />
		<property name="password" value="123456" />
	</bean>
	

	<!-- MaBatis的工厂 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 配置数据源 -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- 配置mybatis核心配置文件 -->
		<property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
	</bean>

	<!-- Mapper动态代理开发 -->
	<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
		<!-- 配置工厂 -->
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
		<!-- 配置接口 -->
		<property name="mapperInterface"  value="com.itheima.mybatis.mapper.UserMapper"></property>
	</bean>
	
</beans>

4.log4j.properties(日志,可有可无)

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

5.UserMapper接口

package com.itheima.mybatis.mapper;

import com.itheima.mybatis.pojo.User;

public interface UserMapper {

	public User selectUserById(int id);
}

UserMapper.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.itheima.mybatis.mapper.UserMapper">
	
	<select id="selectUserById" parameterType="int" resultType="User">
		select * from user where id = #{id}
			
	</select>


</mapper>

改进

applicationContext.xml

每次有新的Mapper时,你都需要在applicationContext.xml里面配置一个bean,使操作繁琐化,于是,采用新的Mapper动态代理,优点:不用再指定具体的SqlSessionFactory的名字,在实例化的瞬间,他被放入spring容器中,然后就可以自动扫描到。

<?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">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/wzl" />
		<property name="username" value="root" />
		<property name="password" value="123456" />
	</bean>
	

	<!-- MaBatis的工厂 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 配置数据源 -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- 配置mybatis核心配置文件 -->
		<property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
	</bean>
	<!-- Mapper动态代理开发 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage"  value="com.itheima.mybatis.mapper"></property>
	</bean>

	<!-- Mapper动态代理开发 -->
	<!-- bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
		<property name="mapperInterface"  value="com.itheima.mybatis.mapper.UserMapper"></property>
	</bean> -->
	
</beans>

 更换(上面的获取实例的方式不可以用了)

//		UserMapper userMapper = (UserMapper)ac.getBean("userMapper");
		
		UserMapper userMapper = (UserMapper)ac.getBean(UserMapper.class);

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值