MyBatis总结(一)
一、创建测试项目工程
二、导包(一个myBatis所需的包,以及一个数据库操作的包)
三、创建实体类
四、配置文件的建立(最佳命名为(SqlMapConfig.xml))
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="datasource.properties"></properties>
<typeAliases>
<typeAlias type="com.lovo.pojo.User" alias="User"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/lovo/mapper/UserMapper.xml"/>
</mappers> </configuration>
用以上方式配置需要先创立配置文件(datasource.properties)
配置文件代码如下:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/myBatis?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=123
然后创建一个获得连接对象的类,我习惯命名为DbUtil放在util包中,代码如下:
public class DbUtil {
private static SqlSessionFactory sessionFactory = null;
private DbUtil() {
InputStream in = null;
try {
in = Resources.getResourceAsStream("SqlMapConfig.xml");
sessionFactory = new SqlSessionFactoryBuilder().build(in);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static SqlSession getSession() {
if (sessionFactory == null) {
new DbUtil();
}
return sessionFactory.openSession();
}
然后再对实体pojo配置一个XML文件和一个接口:
XML最好与接口同名,以便自己好辨识
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与mapper接口相对应 -->
<mapper namespace="com.lovo.mapper.UserMapper">
<resultMap type="User" id="UserMap">
<id property="id" column="id" javaType="int"/>
<result property="userName" column="username" javaType="java.lang.String"/>
<result property="userPwd" column="pwd" javaType="java.lang.String"/>
<result property="state" column="state" javaType="int"/>
</resultMap> <insert id="addUser" parameterType="User">
insert into user values(null,#{User.userName},#{User.userPwd},#{User.state}) </insert>
<delete id="delUserById" parameterType="int">
delete from user where id=#{id}
</delete>
<select id="findUserById" parameterType="int" resultMap="UserMap">
select * from user where id=#{id}
</select>
<update id="updateUser" parameterType="User">
update user set username=#{User.userName},pwd=#{User.userPwd},state=#{User.state} where id=#{User.id}
</update> <select id="FindAllUser" resultMap="UserMap">
select * from user
</select> <select id="findUserCount" resultType="int">
select count(*) from user </select>
<select id="FindUserByState" resultMap="UserMap">
select * from user where state=#{state} </select>
<select id="sumOfState" resultType="int">
select sum(state) from user
</select> </mapper>
接口如下:
public interface UserMapper {
public int addUser(@Param("User") User user);
public int delUserById(@Param("id") int id);
public int updateUser(@Param("User") User user);
public User findUserById(@Param("id") int id);
public List<User> FindAllUser();
public int findUserCount();
public List<User> FindUserByState(@Param("state") int state);
public int sumOfState();
}
这里的@param("User")为别名,除参数只有单个,且为基本数据类型外,都需要给于别名。
最后在建立service层
如:
public class UserServiceImpl implements UserService {
SqlSession sqlSession=DbUtil.getSession();
UserMapper mapper=sqlSession.getMapper(UserMapper.class);
@Override
public int addUser(User user) {
// TODO Auto-generated method stub
int num=0;
num=mapper.addUser(user);
sqlSession.commit();
return num;
}
MyBatis总结(一)的更多相关文章
- 【分享】标准springMVC+mybatis项目maven搭建最精简教程
文章由来:公司有个实习同学需要做毕业设计,不会搭建环境,我就代劳了,顺便分享给刚入门的小伙伴,我是自学的JAVA,所以我懂的.... (大图直接观看显示很模糊,请在图片上点击右键然后在新窗口打开看) ...
- Java MyBatis 插入数据库返回主键
最近在搞一个电商系统中由于业务需求,需要在插入一条产品信息后返回产品Id,刚开始遇到一些坑,这里做下笔记,以防今后忘记. 类似下面这段代码一样获取插入后的主键 User user = new User ...
- [原创]mybatis中整合ehcache缓存框架的使用
mybatis整合ehcache缓存框架的使用 mybaits的二级缓存是mapper范围级别,除了在SqlMapConfig.xml设置二级缓存的总开关,还要在具体的mapper.xml中开启二级缓 ...
- 【SSM框架】Spring + Springmvc + Mybatis 基本框架搭建集成教程
本文将讲解SSM框架的基本搭建集成,并有一个简单demo案例 说明:1.本文暂未使用maven集成,jar包需要手动导入. 2.本文为基础教程,大神切勿见笑. 3.如果对您学习有帮助,欢迎各种转载,注 ...
- mybatis plugins实现项目【全局】读写分离
在之前的文章中讲述过数据库主从同步和通过注解来为部分方法切换数据源实现读写分离 注解实现读写分离: http://www.cnblogs.com/xiaochangwei/p/4961807.html ...
- MyBatis基础入门--知识点总结
对原生态jdbc程序的问题总结 下面是一个传统的jdbc连接oracle数据库的标准代码: public static void main(String[] args) throws Exceptio ...
- Mybatis XML配置
Mybatis常用带有禁用缓存的XML配置 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ...
- MyBatis源码分析(一)开篇
源码学习的好处不用多说,Mybatis源码量少.逻辑简单,将写个系列文章来学习. SqlSession Mybatis的使用入口位于org.apache.ibatis.session包中的SqlSes ...
- (整理)MyBatis入门教程(一)
本文转载: http://www.cnblogs.com/hellokitty1/p/5216025.html#3591383 本人文笔不行,根据上面博客内容引导,自己整理了一些东西 首先给大家推荐几 ...
- MyBatis6:MyBatis集成Spring事物管理(下篇)
前言 前一篇文章<MyBatis5:MyBatis集成Spring事物管理(上篇)>复习了MyBatis的基本使用以及使用Spring管理MyBatis的事物的做法,本文的目的是在这个的基 ...
随机推荐
- spring中用到哪些设计模式
1.工厂模式,这个很明显,在各种BeanFactory以及ApplicationContext创建中都用到了: 2.模版模式,这个也很明显,在各种BeanFactory以及ApplicationCon ...
- size_t 和 size_type的区别
为了使自己的程序有很好的移植性,c++程序员应该尽量使用size_t和size_type而不是int, unsigned 1. size_t是全局定义的类型:size_type是STL类中定义的类型属 ...
- input标签file的value属性IE兼容性问题
在IE中input标签file的value属性是只读的,不能通过js来改变,如下代码在IE中就是无效的: var input = document.getElementById('file'); in ...
- No row with the given identifier exists:错误另解
这是一个hibernate常见的问题.搜索出来最多的答案都是如下面这篇文章所述: http://blog.csdn.net/eyejava/article/details/1896492 但我觉得我问 ...
- delphi强制WebBrowser控件使用指定版本显示网页
function TFrmmain.WriteAppNameToReg:Boolean; var reg:TRegistry; sPath,sAppName:String; Sver:string; ...
- 20161014001 DataGridView 单元格内容 自动计算
private void T_Form_CY_CBD_D_CellValueChanged(object sender, DataGridViewCellEventArgs e) { ...
- CentOS 7 下,如何设置DNS服务器
在CentOS 7下,手工设置 /etc/resolv.conf 里的DNS,过了一会,发现被系统重新覆盖或者清除了.和CentOS 6下的设置DNS方法不同,有几种方式: 1.使用全新的命令行工具 ...
- /usr/include/features.h:367:25:fatal errorXXXXXX类似这种问题
解决方案: sudo apt-get install g++=multilib //至于为什么还没搞清楚,搞清楚在写上来吧!
- Thread 和 Runnable 的区别
在java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnable接口: Thread类是在java.lang包中定义 的.一个类只要继承了Thread类同时覆写了本类中的run ...
- FontMetrics属性的介绍
1.基准点是baseline 2.ascent:是baseline之上至字符最高处的距离 3.descent:是baseline之下至字符最低处的距离 4.leading:是上一行字符的descent ...