Spring3+Mybatis在Maven环境下的整合

这两天不是很忙,最近自己也一直在看Maven相关的东西,了解下关于Maven提供的一些功能:包的依赖管理等,今天就把Spring3.X+Mybatis在Maven环境下整合了一回,最后mvn test成功!闲话少说,直接上代码。

1、新建一个maven project,这个就略过

2、在pom.xml文件中添加依赖

<dependencies>
    <dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		<version>4.7</version>
		<scope>test</scope>
    </dependency>
    <dependency>
		<groupId>org.mybatis</groupId>
		<artifactId>mybatis</artifactId>
		<version>3.0.4</version>
		<type>jar</type>
		<scope>compile</scope>
    </dependency>
    <dependency>
    	<groupId>org.mybatis</groupId>
    	<artifactId>mybatis-spring</artifactId>
    	<version>1.0.0-RC3</version>
    	<type>jar</type>
    	<scope>compile</scope>
    </dependency>
    <dependency>
    	<groupId>mysql</groupId>
    	<artifactId>mysql-connector-java</artifactId>
    	<version>5.1.16</version>
    	<type>jar</type>
    	<scope>compile</scope>
    </dependency>
    <dependency>		<!--commons-dbcp,测试过程中,用1.4版的出现问题,而选择1.2.2版本的没问题-->
    	<groupId>commons-dbcp</groupId>
    	<artifactId>commons-dbcp</artifactId>
    	<version>1.2.2</version>
    	<type>jar</type>
    	<scope>compile</scope>
    </dependency>
    <dependency>
    	<groupId>commons-collections</groupId>
    	<artifactId>commons-collections</artifactId>
    	<version>3.2.1</version>
    	<type>jar</type>
    	<scope>compile</scope>
    </dependency>
</dependencies>

 2、编写测试代码:src/test/java/org/clzps/main/MybatisSpringTest.java,肯定不通过

public void testMain() 
{
	ApplicationContext context = new ClassPathXmlApplicationContext("spring-ds.xml");
	UserDAO userMapper = (UserDAO)context.getBean("userDAO");
	System.out.println(userMapper.getClass().getName());
	User user = userMapper.queryUserById(1);
	System.out.println(user.getUsername());
}

 3、增加Spring的资源文件:src/main/resources/spring-ds.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
	<bean id="userDAO" class="org.clzps.dao.impl.UserDAOImpl">
		<property name="sqlSessionFactory" ref="sqlSessionFactory" />
	</bean>
	
	<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/maven?characterEncoding=UTF-8" />
		<property name="username" value="root" />
		<property name="password" value="root" />
	</bean>
	
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="ibatis/sqlMapConfig.xml" />
	</bean>	
</beans>

 4、增加src/main/resources/ibatis/sqlMapConfig.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>
	<typeAliases>
		<typeAlias alias="User" type="org.clzps.model.User" />  <!--配置别名,是在Mapper中使用别名-->
	</typeAliases>
	<mappers>
		<mapper resource="ibatis/UserMapper.xml"/>
	</mappers>
</configuration>

 5、在增加UserMapper.xml之间,在Java代码中生成对应的model,即src/main/java/org/clzps/model/User.java

 

public class User {
	private int id;
	private String username;
	private String password;
	private int age;
	
	//... ...getter and setter
}

 6、增加/src/main/resources/ibatis/UserMapper.xml,对应与Model的Mapper文件

<?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="org.clzps.model.UserMapper">  <!--mybatis的namespace是必须的-->
	<select id="queryUserByID" parameterType="int" resultType="User">  <!--resultType使用的是简称-->
		SELECT * 
		FROM maven.User
		WHERE id = #{id}
	</select>
</mapper>

 7、建立src/main/java/org/clzps/dao包,建立interface文件,UserDAO.java

public interface UserDAO {
	public User queryUserById(int id);
}

 8、实现UserDAO接口:src/main/java/org/clzps/dao/impl/UserDAOImpl.java,并且调用UserMapper中的select

public class UserDAOImpl extends SqlSessionDaoSupport implements UserDAO {

	public User queryUserById(int id) {
		return (User)this.getSqlSession().selectOne("org.clzps.model.UserMapper.queryUserByID", id);
	}
}

 9、至此代码编写完成,运行mvn test;更多功能挖掘,待以后完善...

你可能感兴趣的:(spring,maven,mysql,bean,ibatis)