为了方便看,把所有文件都放一个文件夹里了。 总共是5个文件: 1.Mybatis.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD SQL Map Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<mappers>
<mapper resource="bs/UserMapper.xml" />
<!-- 连接配置文件 -->
</mappers>
</configuration>
2.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="UserMapper">
<select id="insert" parameterType="Map" resultType="java.util.Map">
INSERT INTO user
(id)
VALUES
(#{id})
</select>
<select id="findById" parameterType="int" resultType="java.util.Map">
SELECT *
FROM user
WHERE id=#{id}
</select>
<!-- <select id="findList" resultType="mybatis.User" parameterType="int">
SELECT * FROM user
<if test="_parameter!=null and _parameter!=' '">
WHERE uid=#{_parameter}
</if>
</select> -->
</mapper>
3.spring.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>org.gjt.mm.mysql.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/zdy?useUnicode=true&characterEncoding=UTF-8
</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>1111</value>
</property>
</bean>
<!-- 获取会话工厂,并注入Mybatis,和dateSource数据库链接 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="bs/Mybatis.xml"></property>
</bean>
<!-- 在会话工厂中取出SqlSessionTemplate这个对象 -->
<bean id="sqlsessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<!-- 把sqlsessionTemplate注入到UserMapper中去。UserMapper才能对数据进行操作 -->
<bean id="user" class="bs.UserImpl">
<property name="sqlsession" ref="sqlsessionTemplate">
</property>
</bean>
</beans>
4.UserImpl.java:操作数据库工具类
package bs;
import java.util.List;
import java.util.Map;
import org.mybatis.spring.SqlSessionTemplate;
public class UserImpl {
//注入以后就可以直接使用sqlsession
private SqlSessionTemplate sqlsession;
public void setSqlsession(SqlSessionTemplate sqlsession) {
this.sqlsession = sqlsession;
}
//用sqlsession去操作数据库
public void insert(Map user){
sqlsession.insert("UserMapper.insert", user);
}
public void findById(int id){
sqlsession.selectOne("UserMapper.findById", id);
System.out.println(sqlsession.selectOne("UserMapper.findById", id));
//因为输出值是一个map集合,所以打印一个map
}
}
5.Test.java
package bs;数据库内容
import java.util.Map;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("bs/spring.xml");
UserImpl userImpl = (UserImpl)ctx.getBean("user");
//固定格式 取出bean中创建的UserMapper对象
userImpl.findById(1);
}
}
查询出来内容