您的位置:首页 > 编程语言 > Java开发

spring整合mybatis

2016-05-25 18:04 309 查看
先创建表,并且插入了一条数据

create table user(
id int auto_increment primary key,
name varchar(64),
pwd varchar(64)
);
insert into user values(1,'root','123');


利用:mybatis-generator-core-1.3.2自动生成User.java、UserMapper.xm、UserMapper.java(名字自己可以修改)

mybatis-generator-core-1.3.2下载链接:http://download.csdn.net/detail/xia744510124/9531103

利用MyBatis Generator自动创建代码:

参考博文:http://blog.csdn.net/zhshulin/article/details/23912615

maven pom.xml内容

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.xll.mybatis</groupId>
<artifactId>MybatisDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MybatisDemo</name>
<url>http://maven.apache.org</url>
<build>
<finalName>mavenproject</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<!-- mybatis核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.0</version>
</dependency>
<!-- mybatis/spring包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<!-- 上传组件包 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.2.6.RELEASE</version>
</dependency>
<!--必须,否则得报dataSource classNotFound错误-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.6.RELEASE</version>
</dependency>
</dependencies>
</project>


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"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--<context:annotation-config /> -->
<!-- 注解的类转换为bean -->
<context:component-scan base-package="com.xll" />
<!-- 事物注解扫描 -->
<tx:annotation-driven transaction-manager="txManager" />
<!-- 引入jdbc.properties配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<!-- 配置数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</bean>
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com/xll/mapping/*.xml"></property>
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xll.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>


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" >
<!--namespace要与实际的Dao接口名一致(全限定名)-->
<mapper namespace="com.xll.dao.UserDao">
<!--type要与实际的表映射类名一致(全限定名)-->
<resultMap id="BaseResultMap" type="com.xll.pojo.User">
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="pwd" property="pwd" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List">
id, name, pwd
</sql>
<!--要与接口中方法名一致-->
<select id="selectByPrimaryKey" resultMap="BaseResultMap"
parameterType="java.lang.Integer">
select
<include refid="Base_Column_List" />
from user
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from user
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.xll.pojo.User">
insert into user (id, name,
pwd
)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR},
#{pwd,jdbcType=VARCHAR}
)
</insert>
<insert id="insertSelective" parameterType="com.xll.pojo.User">
insert into user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="name != null">
name,
</if>
<if test="pwd != null">
pwd,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
<if test="pwd != null">
#{pwd,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.xll.pojo.User">
update user
<set>
<if test="name != null">
name = #{name,jdbcType=VARCHAR},
</if>
<if test="pwd != null">
pwd = #{pwd,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.xll.pojo.User">
update user
set
name = #{name,jdbcType=VARCHAR},
pwd = #{pwd,jdbcType=VARCHAR}
where id
= #{id,jdbcType=INTEGER}
</update>
</mapper>


User.java内容

package com.xll.pojo;
public class User {
private Integer id;
private String name;

private String pwd;

public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd == null ? null : pwd.trim();
}
@Override
public String toString() {
return "id = " + id + " name = " + name;
}
}


UserDao.java(用mybatis-generator自动生成,并且不用编写实现类)

package com.xll.dao;
import com.xll.pojo.User;
public interface UserDao {
int deleteByPrimaryKey(Integer id);
int insert(User record);
int insertSelective(User record);
User selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
}


UserService 接口

package com.xll.service;
import com.xll.pojo.User;
public interface UserService {
User selectByPrimaryKey(Integer id);
}


UserServiceImpl.java实现类

import com.xll.dao.UserDao;
import com.xll.pojo.User;
import com.xll.service.UserService;
@Service
public class UserServiceImpl implements UserService {
@Resource
private UserDao userDao;
@Override
public User selectByPrimaryKey(Integer id) {
User user = userDao.selectByPrimaryKey(id);
return user;
}
}


测试类TestMybatis.java

package com.xll.mybatis;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.xll.pojo.User;
import com.xll.service.UserService;
import com.xll.serviceImpl.UserServiceImpl;
public class TestMybatis {
private UserService userServiceImpl;
@Test
public void test() {
// 使用"spring.xml"和"spring-mybatis.xml"这两个配置文件创建Spring上下文
// ApplicationContext ac = new ClassPathXmlApplicationContext(new
// String[]{"spring.xml","spring-mybatis.xml"});
ApplicationContext acc = new ClassPathXmlApplicationContext("applicationContext.xml");
userServiceImpl = (UserServiceImpl) acc.getBean("userServiceImpl");
User user = userServiceImpl.selectByPrimaryKey(1);
System.out.println(user.toString());
((AbstractApplicationContext) acc).close();
}
}


测试结果图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring mybatis