spring整合mybatis
2016-05-25 18:04
309 查看
先创建表,并且插入了一条数据
利用: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内容
applicationContext.xml内容
UserMapper.xml内容
User.java内容
UserDao.java(用mybatis-generator自动生成,并且不用编写实现类)
UserService 接口
UserServiceImpl.java实现类
测试类TestMybatis.java
测试结果图:
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(); } }
测试结果图:
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Spring和ThreadLocal
- Spring Boot 开发微服务
- Spring整合Quartz(JobDetailBean方式)
- Spring整合Quartz(JobDetailBean方式)
- 模拟Spring的简单实现
- Spring整合WebSocket应用示例(上)
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- 深入浅析mybatis oracle BLOB类型字段保存与读取
- springmvc 发送ajax出现中文乱码的解决方法汇总
- MyBatis MapperProvider MessageFormat拼接批量SQL语句执行报错的原因分析及解决办法
- SpringMVC框架下JQuery传递并解析Json格式的数据是如何实现的
- struts2 spring整合fieldError问题
- spring的jdbctemplate的crud的基类dao
- 读取spring配置文件的方法(spring读取资源文件)
- SpringMVC整合mybatis实例代码