这里以增删改查user table为例
1 首先导入相关的jar包文件,如下:
导入的包:commons- dbcp,commons-logging,commons-pool,mybatis-3.0.5,mybatis-spring,mysql-connector-java,spring,spring-aop,spring-beans,spring-context,spring-core,spring-expression,spring-tx
2 然后确定pojo对象,User.java
public class User implements Serializable {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
3 用接口形式调用映射文件UserMapper.java
public interface UserMapper {
User getUser(String username);
User getUserList();
@Select("select * from User where username=#{username}")
User getUserAnnotation(String username);
}
4 书写操作数据库的sql语句,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.cienet.bjk.persistence.UserMapper">
<cache />
<select id="getUser" parameterType="string" resultType="User">
SELECT
* from user where
</select>
<select id="getUserList" resultType="list">
SELECT
* from user
</select>
</mapper>
代码解释:namespace为映射的interface文件的位置,这个必须和上面的UserMapper.xml文件放在同一个目录下,且名称要一样,不然会出现找不到文件的错误。
select标签中的id表示与UserMapper.java中的方法对应。
parameterType表示传入参数的类型,resultType表示返回结果的类型#{}表示传入参数。
5 业务逻辑层代码
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User getUser(String username){
return userMapper.getUser(username);
}
public User getUserAnnotation(String username){
return userMapper.getUser(username);
}
@SuppressWarnings("unchecked")
public List<User> getUserList() {
return (List<User>) userMapper.getUserList();
}
}
6 然后在spring配置以注解方式运行,这样的好处是不用为每个pojo写对应的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: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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfi
<property name="locations">
<value>classpath:com/cienet/bjk/config/jdbc.properties</value>
</property>
</bean>
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="sqlSessionFactory">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="com.cienet.bjk.model" />
</bean>
<bean id="transactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- scan for mappers and let them be autowired -->
<bean>
<property name="basePackage" value="com.cienet.bjk.persistence" />
</bean>
<context:component-scan base-package="com.cienet.bjk.service" />
<context:annotation-config />
<tx:annotation-driven />
</beans>
7 最后,写测试代码进行验证是否运行成功。
@Test
public void testUsert1(){
ctx = new ClassPathXmlApplicationC
"com/cienet/bjk/config/beans.xml");
UserService user = (UserService) ctx.getBean("userService");
System.out.print(user.getUser("123").getPassword());
//System.out.print(user.getUserList().get(0).getPassword());
System.out.println(user.getUserAnnotation("123").getPassword());//invoke the annotation
UserMapper um = sqlSession.getMapper(UserMapper.class);
System.out.println(um.getUserAnnotation("123").getPassword());
}
运行成功最后。其中System.out.println(user.getUserAnnotation("123").getPassword());//invoke the annotation
表示用注解的方式访问访问数据库,这种形式允许直接把sql语句写在java文件中,不过,不推荐这种方式,因为java文件会过于臃肿,不好维护。