spring项目基本的xml配置

1.加入jar包

    <dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>5.0.7.RELEASE</version>
		</dependency>


		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>5.0.6.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>5.0.7.RELEASE</version>

		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.8.9</version>
		</dependency>

		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.3.2</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.4.4</version>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.38</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>5.0.7.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>5.0.7.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-dbcp2</artifactId>
			<version>2.1.1</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>5.0.7.RELEASE</version>
		</dependency>

	</dependencies>

2.一些配置文件

在spring-mybatis.xml文件中配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:task="http://www.springframework.org/schema/task"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/task
	http://www.springframework.org/schema/task/spring-task.xsd
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/tool 
	http://www.springframework.org/schema/tool/spring-tool.xsd
	http://www.springframework.org/schema/context  
	http://www.springframework.org/schema/context/spring-context.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx.xsd">


	<bean id="dataSource"
		class="org.apache.commons.dbcp2.BasicDataSource"

		destroy-method="close">

		<property name="driverClassName"
			value="${mysql.driverClassName}" />

		<property name="url" value="${mysql.url}" />

		<property name="username" value="${mysql.username}" />

		<property name="password" value="${mysql.password}" />

	</bean>

	<bean

		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

		<property name="locations" value="classpath:jdbc.properties" />

	</bean>

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

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

		<!-- 优化: 不再需要 mybatis的配置文件, 配置mybatis中别名. mapper的映射文件的位置 -->

		<property name="mapperLocations"
			value="classpath*:com/adeline/project/mySpring/mapper/*.xml" />

		<property name="typeAliasesPackage"> <!-- SqlSessionFactoryBean中定义 -->

			<array>

				<value>com.adeline.project.mySpring.domain</value>

			</array>

		</property>

		<!-- 主要用来配置mybatis中的configuration中的setting配置 -->

		<property name="configurationProperties">

			<props>

				<prop key="logImpl">LOG4J</prop>

			</props>

		</property>

	</bean>

	<bean id="sqlSession"
		class="org.mybatis.spring.SqlSessionTemplate">

		<!-- 只能通过 构造方法注入 sqlSessionFactory -->

		<constructor-arg name="sqlSessionFactory"
			ref="sqlSessionFactory"></constructor-arg>

	</bean>

	<!-- 配置事物管理器 -->

	<bean id="txManager"

		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

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

	</bean>

	<!-- 生成事物增强的切面类的对象 -->

	<tx:advice id="txAdvice" transaction-manager="txManager">

	</tx:advice>

	<!--启用事物注解 -->

	<tx:annotation-driven
		transaction-manager="txManager" />
</beans>

引用到的文件jdbc. properties

 
mysql.driverClassName=com.mysql.jdbc.Driver
 
mysql.url=jdbc:mysql://localhost:3306/spring_test
 
mysql.username=root
 
mysql.password=root

3.在spring-mybatis.xml或者另起一个XML文件,将实体类托管给spring

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:task="http://www.springframework.org/schema/task"
	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/task
	http://www.springframework.org/schema/task/spring-task.xsd">
	
	<import resource="classpath:spring-mybatis.xml" />
	
	<bean id="User" class="com.adeline.project.mySpring.domain.User">
		<property name="userId" value="1001"></property>
	</bean>
	
</beans>

 4.测试类

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.adeline.project.mySpring.domain.User;

public class IOCTest {

	@Test
	public void test01() {
        //加载配置文件
		ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-config.xml");
        //获取实体类信息
		User user = (User) context.getBean("User");
		System.out.println(user);
	}

}

 5.测试结果

在配置User实体类时,只设置了userId的属性,所以输出只有userId有值

6.若不想以XML的形式托管实体类,则可以以注解的形式配置

在实体类中加入注解

 

7.测试注解形式

    @Test
	public void test02() {
		// 注解形式获取实体类
		ApplicationContext context = new AnnotationConfigApplicationContext(User.class);
		User user = (User) context.getBean("user");
		user.setUserId(1002);
		System.out.println(user);
	}

测试结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值