标签:
两周前学习mybatis框架,参考了网上多位大神的博客,但因为各种原因(不解释)总是没法成功搭建环境并运行项目。周末花了点时间阅读了文档并整理之前琐碎的内容,解决掉之前遇到的问题。现将整合环境的关键步骤整理成学习手记一篇。
文章只提取了整合环境的主体过程,没有太深入的解析其中内容,若想深入学习mybaits,请自行阅读文档、源码,或参看网上其他大神的博客,本人菜鸟一只,只是做做学习笔记(ps:吃货请直接拖到底部)。
1、导入spring核心包、spring测试包、mybatis核心包、mysql驱动包、druid数据库连接池以及mybatis-spring包(用于整合mybatis和spring):
在pom.xml文件中添加依赖,不详述。
2、数据库建表(新建了一张练习用的简单表格):
建表sql如下:
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT ‘主键‘,
`name` varchar(128) DEFAULT NULL COMMENT ‘姓名‘,
`age` int(11) DEFAULT NULL COMMENT ‘年龄‘,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8
3、创建user表对应的实体类:
package xlkyt.mybatis.model;
/**
* Created by Administrator on 2016/5/6.
*/
public class User {
private int id;
private String name;
private int age;
public User() {
}
public User(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name=‘" + name + ‘\‘‘ +
", age=" + age +
‘}‘;
}
}
4、创建实体类对应的dao:
package xlkyt.mybatis.dao;
import xlkyt.mybatis.model.User;
/**
* Created by Administrator on 2016/5/15.
*/
public interface UserMapper {
/**
*
* @param id
* @return
*/
User getUserById(int id);
/**
*
* @param user
*/
void addUser(User user);
}
5、创建dao对应的mapper.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="xlkyt.mybatis.dao.UserMapper">
<select id="getUserById" parameterType="int" resultType="User" >
SELECT * FROM USER WHERE ID = #{id}
</select>
<insert id="addUser" parameterType="User" >
INSERT INTO USER(name, age) VALUES (#{name}, #{age})
</insert>
</mapper>
6、编写spring配置文件:
文件路径:src/main/resources/spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--引入mybatis配置文件-->
<import resource="classpath:spring-mybatis.xml" />
<!--自动扫描注入-->
<context:component-scan base-package="xlkyt.mybatis.service" />
<!--引入db.properties文件-->
<context:property-placeholder location="classpath:db.properties" />
</beans>
7、编写spring-mybatis配置文件:
文件路径:src/main/resources/spring-mybatis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--配置数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!--配置SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
<!--引用数据源-->
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:xlkyt/mybatis/mapping/*.xml" />
<!--指定实体类包,自动将包内实体的简单类名映射为别名-->
<property name="typeAliasesPackage" value="xlkyt.mybatis.model" />
</bean>
<!--扫描mapping文件对应dao文件所在的包-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
<property name="basePackage" value="xlkyt.mybatis.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<!--配置事务管理-->
</beans>
8、修改maven的pom文件配置(解决idea下,maven项目中src源代码里的xml等资源文件无法编译进classes文件夹的问题):
ps:在idea的maven项目中,默认源代码目录下的xml等资源文件并不会在编译的时候一起打包进classes文件夹,而是直接舍弃掉。如果使用的是eclipse,eclipse的src目录下的xml等资源文件在编译的时候会自动打包进输出到classes文件夹。hibernate、mybatis和spring有时会将配置文件放置在src目录下,编译后要一起打包进classes文件夹,所以存在着需要将xml等资源文件放置在源代码目录下的需求。
解决方式:在pom文件中找到<build>节点,添加下列代码:
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
9、编写service:
package xlkyt.mybatis.service.impl;
import xlkyt.mybatis.dao.UserMapper;
import xlkyt.mybatis.model.User;
import xlkyt.mybatis.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Created by Administrator on 2016/5/15.
*/
@Service("userService")
public class UserServiceImpl implements IUserService {
private UserMapper userMapper;
@Autowired
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
public User getUserById(int id) {
return userMapper.getUserById(id);
}
public void addUser(User user) {
userMapper.addUser(user);
}
}
10、单元测试:
package com.mybatis;
import xlkyt.mybatis.model.User;
import xlkyt.mybatis.service.IUserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Created by Administrator on 2016/5/6.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring.xml")
public class TestMyBatis {
private IUserService userService;
@Autowired
public void setUserService(IUserService userService) {
this.userService = userService;
}
@Test
public void testGetUserById() {
User user = userService.getUserById(3);
System.out.println(user);
}
@Test
public void testAddUser() {
userService.addUser(new User(1, "allen", 18));
}
}
11、测试通过,至此框架整合成功,接下来该干啥干啥。
歪个楼系列:
木屋烧烤有道菜叫香辣烤羊蹄,吃过一次至今念念不忘,第二次回去的时候已经下架,不知道何时能再吃一次,强势推荐一下。夏夜里三两好友、满天繁星,天台烧烤加冰啤,向来绝配,不喝酒的童鞋可以搭配店里的酸梅汁,一样绝配~
未经作者同意,贴个链接:http://s.dianping.com/topic/6343385 不谢。
记得去年还是只菜鸟中的菜鸟,和直系boss一起被同事坑了一道背了黑锅,下午一起闷闷不乐的去南山听讲座,没记错的话是深圳百公里徒步那一天(那天事真多=_=)。完了去烧烤店腐败(白吃白喝)了一顿,其中就点了香辣烤羊蹄搭配酸梅汁,美食过后所有烦恼顿时烟消云散,如今所有的不愉快早已抛诸脑后,只记得当时啃完蹄子后闷一口梅汁的酸爽!
想起个包名的时候想起了那只蹄子,于是包就叫做xlkyt。纪念那只羊,同时感恩那些在你最落魄的时候拉了你一把的人。
标签:
原文地址:http://www.cnblogs.com/tarencez/p/5496586.html