写一个mybatis+spring整合开发的小例子
项目源文件展示
导入mybatis+spring整合的jar包(百度下载)
- 创建实体类、方法接口
//创建实体类的时候,创建的属性名称和mysql的表对应
@Component("test")
public interface test {
public Admin testing(String name);
}
- 创建mybatis配置文件mybatis-config.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="dao"/>
</typeAliases>
<mappers> <!-- 配置映射文件 -->
<mapper resource="mybatis/sql.xml"/>
</mappers>
</configuration>
- 创建mybatis映射文件sql.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="mapper.test"> <!--使用mapper动态开发-->
<select id="testing" parameterType="String" resultType="admin">
select *
from admin
where username = #{name}
</select>
</mapper>
- 创建spring的配置文件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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 外部连接properties文件 -->
<context:property-placeholder location="classpath:mybatis/database.properties"/>
<!-- 配置数据库连接池 -->
<bean id="datasource" class="com.mchange.v2.c3p0.DriverManagerDataSource">
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 配置SqlSession工厂 -->
<bean name="SqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource"></property>
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
</bean>
<!-- 配置mapper动态代理 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mapper"></property>
</bean>
</beans>
- 创建database.properties文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///book_db
jdbc.username=root
jdbc.password=6112783king //后三个,需要改为自己的数据库信息
配置好上面,就已经将mybatis和spring进行整合了。
- 创建testservlet,进行测试
@WebServlet("/testservlet")
public class testservlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
WebApplicationContext wa = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
test bean = (test) wa.getBean("test");
Admin admin = bean.testing("a");
System.out.println("密码是:"+admin.getPassword());
}
}
把项目在Tomcat运行,在浏览器上输入http://localhost:8080/mybatis+spring/testservlet,
在软件后台可以输出对应用户的密码。