首页 > 代码库 > Spring和MyBatis环境整合
Spring和MyBatis环境整合
Spring:
Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。
两个重要模块:Spring 面向方面编程(AOP)和控制反转 (IOC) 容器。
控制反转模式(也称作依赖性介入)的基本概念是:不创建对象,但是描述创建它们的方式。在代码中不直接与对象和服务连接,但在配置文件中描述哪一个组件需要哪一项服务。
容器 (在 Spring 框架中是 IOC 容器) 负责将这些联系在一起。在典型的 IOC 场景中,容器创建了所有对象,并设置必要的属性将它们连接在一起,决定什么时间调用方法。
MyBatis:
链接代码:
model----User.java
1 package com.springMyBatis.system.model; 2 3 public class User { 4 private int id; 5 private String username; 6 private String password; 7 public User(){} 8 public int getId() { 9 return id; 10 } 11 public void setId(int id) { 12 this.id = id; 13 } 14 public String getUsername() { 15 return username; 16 } 17 public void setUsername(String username) { 18 this.username = username; 19 } 20 public String getPassword() { 21 return password; 22 } 23 public void setPassword(String password) { 24 this.password = password; 25 } 26 public String toString(){ 27 return "User[id="+id+" , username="+username+" , password="+password+"]"; 28 } 29 30 31 }
dao------接口UserDao.java
package com.springMyBatis.system.dao; 2 3 import com.springMyBatis.system.model.User; 4 5 public interface UserDao { 6 public User getUser(User user); 7 public void addUser(User user); 8 public void updateUser(User user); 9 public void deleteUser(int UserId); 10 }
dao-----UserDao.xml
<?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 4 <mapper namespace="com.springMyBatis.system.dao.UserDao"> 5 <select id="getUser" parameterType="com.springMyBatis.system.model.User" resultType="com.springMyBatis.system.model.User"> 6 SELECT * FROM user WHERE username=#{username} AND password=#{password} 7 </select> 8 <insert id="addUser" parameterType="com.springMyBatis.system.model.User" flushCache="true"> 9 INSERT INTO user (id,username,password) VALUES (#{id},#{username},#{password}) 10 </insert> 11 <update id="updateUser" parameterType="com.springMyBatis.system.model.User"> 12 UPDATE user SET password=#{password} WHERE id=#{id} 13 </update> 14 <delete id="deleteUser" parameterType="int"> 15 DELETE FROM user WHERE id=#{id} 16 </delete> 17 </mapper>
<?xml version="1.0" encoding="UTF-8"?>
2 <beans
3 xmlns="http://www.springframework.org/schema/beans"
4 xmlns:tx="http://www.springframework.org/schema/tx"
5 xmlns:p="http://www.springframework.org/schema/p"
6 xmlns:aop="http://www.springframework.org/schema/aop"
7 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
8 xsi:schemaLocation="http://www.springframework.org/schema/beans
9 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
10 http://www.springframework.org/schema/tx
11 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
12 http://www.springframework.org/schema/aop
13 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
14 <!-- 配置数据源-->
15 <bean id="jdbcDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
16 <property name="driverClassName">
17 <value>org.gjt.mm.mysql.Driver</value>
18 </property>
19 <property name="url">
20 <value>jdbc:mysql://localhost:3306/springmybaitis?useUnicode=true&characterEncoding=UTF-8</value>
21 <!--springmybaitis是我的数据库 -->
22 </property>
23 <property name="username">
24 <value>root</value>
25 </property>
26 <property name="password">
27 <value>123456</value>
28 </property>
29 </bean>
30 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
31 <property name="dataSource" ref="jdbcDataSource" />
32 <property name="configLocation" value="http://www.mamicode.com/classpath:mybatis-config.xml"></property>
33 </bean>
34 <bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
35 <property name="mapperInterface" value="http://www.mamicode.com/com.springMyBatis.system.dao.UserDao"></property>
36 <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
37 </bean>
38 </beans>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration PUBLIC 3 "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <mappers> 7 <mapper resource="com/springMyBatis/system/dao/UserDao.xml"/> 8 </mappers> 9 </configuration>
6、测试
首先测试添加一条数据:
1 package com.springMyBatis.system.test; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 import com.springMyBatis.system.dao.UserDao; 7 import com.springMyBatis.system.model.User; 8 9 public class UserController { 10 11 /** 12 * @param args 13 */ 14 public static void main(String[] args) { 15 ApplicationContext ctx=null; 16 ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); 17 UserDao userDao=(UserDao) ctx.getBean("userDao"); 18 User user=new User(); 19 //添加两条数据 20 user.setId(1); 21 user.setUsername("Jessica"); 22 user.setPassword("123"); 23 userDao.addUser(user); 24 user.setId(2); 25 user.setUsername("Jessica2"); 26 user.setPassword("123"); 27 userDao.addUser(user); 28 System.out.println("添加成功"); 29 //查询数据 30 user.setUsername("Jessica"); 31 user.setPassword("123"); 32 System.out.println(userDao.getUser(user).toString()); 33 user.setUsername("Jessica2"); 34 user.setPassword("123"); 35 System.out.println(userDao.getUser(user).toString()); 36 //修改数据 37 user.setId(2); 38 user.setPassword("802"); 39 userDao.updateUser(user); 40 System.out.println("修改成功"); 41 //删除数据 42 userDao.deleteUser(1); 43 System.out.println("删除成功"); 44 45 } 46 47 }
Spring和MyBatis环境整合