Spring和MyBatis环境整合
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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="classpath:mybatis-config.xml"></property>
33 </bean>
34 <bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
35 <property name="mapperInterface" value="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>
以上是关于Spring和MyBatis环境整合的主要内容,如果未能解决你的问题,请参考以下文章