参考文档:
http://www.cnblogs.com/jyh317/p/3834142.html http://www.cnblogs.com/xdp-gacl/p/4271627.html http://nirvana1988.iteye.com/blog/971246 http://blog.csdn.net/lemonyfei/article/details/8925325 http://blog.csdn.net/daxiang12092205/article/details/27561503 http://www.cnblogs.com/zhangzhifeng/p/4260573.html http://www.itnose.net/detail/6107526.html
工程目录结构:
各文件:
IUserDao.java
package com.springMyBatis.system.dao; import com.springMyBatis.system.model.User; public interface IUserDao { public User getUser(User user); public int addUser(User user); public void updateUser(User user); public void deleteUser(int UserId); }
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.springMyBatis.system.dao.IUserDao"> <select id="getUser" parameterType="com.springMyBatis.system.model.User" resultType="com.springMyBatis.system.model.User"> SELECT * FROM employees WHERE id=#{id} </select> <insert id="addUser" parameterType="com.springMyBatis.system.model.User" flushCache="true"> INSERT INTO employees (name,gender,salary) VALUES (#{name},#{gender},#{salary}) </insert> <update id="updateUser" parameterType="com.springMyBatis.system.model.User"> UPDATE employees SET password=#{name}, gender=#{gender}, salary=#{salary} WHERE id=#{id} </update> <delete id="deleteUser" parameterType="int"> DELETE FROM employees WHERE id=#{id} </delete> </mapper>
User.java
package com.springMyBatis.system.model; public class User { private int id; private String name; private String gender; private int salary; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } @Override public String toString() { return "id=" + id + " name=" + name + " gender=" + gender + " salary=" + salary; } }
UserController.java
package com.springMyBatis.system.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.springMyBatis.system.model.User; import com.springMyBatis.system.dao.IUserDao; public class UserController { /** * @param args */ public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); IUserDao userDao = (IUserDao) ctx.getBean("userDao"); User user = new User(); user.setName("harry"); user.setGender("male"); user.setSalary(6000); int result = userDao.addUser(user); System.out.println("result = " + result); } }
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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:property-placeholder location="classpath:jdbc.properties" /> <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"> <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" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis-config.xml" /> </bean> <bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.springMyBatis.system.dao.IUserDao" /> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> </beans>
jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test jdbc.username=test jdbc.password=test
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> <mappers> <mapper resource="com/springMyBatis/system/mapper/UserMapper.xml"/> </mappers> </configuration>