mybatis呢是一个orm数据库框架,非常适合新人学,门槛相对较低
本人呢曾经是先做的hibernate,后接触的mybatis,接触mabatis前我比较抵触,为啥呢,
当时喜欢hibernate的POJO,直接注解实体映射数据库表对象,要增删改造直接get(id),save(entity),delete(entity),update(entity),显得非常方便;
而且又支持hql的查询
但是,久而久之,很多数据库性能方面的问题会暴露出来,你必须直接优化sql语句,那么hibernate的弊端就出现了
虽然mybatis需要手写sql语句,其实也挺不错,字段映射就OK了,多写写sql也是有好处滴
那有的人会问了,什么时候用hibernate,什么时候用mybatis呢?
回答曰:看项目呗,如果项目挺大,那就是mybatis,如果项目相对较小,是个后台管理系统,对数据的查询量不是特别多,那就用hibernate呗
或者干脆每个项目都用mybatis都行啊~!
除此之外,也有部分企业使用的自己开发的orm框架,连数据源都是自己的,那这样当然最好
好吧,废话有点多,咱写代码的还是直接贴码吧:
(头几篇博文不会使用maven,后面的ssm整合会使用maven来进行管理)
工程结构:
asm-3.3.1.jar
cglib-2.2.2.jarcommons-logging-1.1.1.jarjavassist-3.17.1-GA.jarlog4j-1.2.17.jarlog4j-api-2.0-beta9.jarlog4j-core-2.0-beta9.jarmybatis-3.2.3.jarmysql-connector-java-5.1.7-bin.jarslf4j-api-1.7.5.jarslf4j-log4j12-1.7.5.jar
SqlMapConfig.xml,这个可以理解为连接mybatis和数据库的媒介,以及一些mybatis配置都在这个文件里
1 2 56 7 8 18 19 209 1710 11 1612 13 14 15 21 2322
User.xml 这个文件就是正对user实体进行的相关查询,命名同User.java
1 2 5 6 78 9 19 24 25 26 27 33 36 37 38 41 42 43 49 50 56 57 58 5951 select LAST_INSERT_ID()52 53 54 insert into user(name,age,sex) values(#{name},#{age},#{sex})5560 delete from user where id=#{id}61 62 63 6667 update user set name=#{name},age=#{age},sex=#{sex} where id=#{id}68 69 70 71
User.java
1 package com.mybatis001.bean; 2 3 public class User { 4 5 private int id; 6 private String name; 7 private int age; 8 private String sex; 9 10 public User() {11 super();12 }13 14 public User(String name, int age, String sex) {15 super();16 this.name = name;17 this.age = age;18 this.sex = sex;19 }20 21 public int getId() {22 return id;23 }24 public void setId(int id) {25 this.id = id;26 }27 public String getName() {28 return name;29 }30 public void setName(String name) {31 this.name = name;32 }33 public int getAge() {34 return age;35 }36 public void setAge(int age) {37 this.age = age;38 }39 public String getSex() {40 return sex;41 }42 public void setSex(String sex) {43 this.sex = sex;44 }45 46 @Override47 public String toString() {48 return "User [id=" + id + ", name=" + name + ", age=" + age + ", sex="49 + sex + "]";50 }51 52 }
UserDao.java
1 package com.mybatis001.dao; 2 3 import java.util.List; 4 5 import com.mybatis001.bean.User; 6 7 public interface UserDao { 8 9 public User findUserById(int id) throws Exception;10 11 public ListfindUserList(String name) throws Exception;12 13 public Integer insertUser(User user) throws Exception;14 15 public void deleteUser(int id) throws Exception;16 17 public void updateUser(User user) throws Exception;18 19 }
UserDaoImpl.java
1 package com.mybatis001.dao.impl; 2 3 import java.util.List; 4 5 import org.apache.ibatis.session.SqlSession; 6 import org.apache.ibatis.session.SqlSessionFactory; 7 8 import com.mybatis001.bean.User; 9 import com.mybatis001.dao.UserDao;10 11 public class UserDaoImpl implements UserDao {12 13 // 注入SqlSessionFactory14 private SqlSessionFactory sqlSessionFactory;15 16 public UserDaoImpl(SqlSessionFactory sqlSessionFactory) {17 this.sqlSessionFactory = sqlSessionFactory;18 }19 20 @Override21 public User findUserById(int id) throws Exception {22 23 // 根据SqlSessionFactory创建SqlSession24 25 SqlSession sqlSession = sqlSessionFactory.openSession();26 27 // 通过sqlSession查询用户信息(发起数据库操作)28 // 第一个参数statement:指定mapper映射文件中statement的id,指定 时需要前边加上statement所属的命名空间29 // 第二个参数parameter,指定 输入参数30 // selectOne返回的是单条记录,如果select返回多条记录(list集合),使用selectOne会报错31 // 根据映射文件中的resultType指定输出类型32 User user = sqlSession.selectOne("test.findUserById", id);33 return user;34 }35 36 @Override37 public ListfindUserList(String name) throws Exception {38 SqlSession sqlSession = sqlSessionFactory.openSession();39 List userList = sqlSession.selectList("test.findUserList", name);40 return userList;41 }42 43 @Override44 public Integer insertUser(User user) throws Exception {45 SqlSession sqlSession = sqlSessionFactory.openSession();46 int lastUserId = sqlSession.insert("test.insertUser", user);47 48 sqlSession.commit();49 sqlSession.close();50 return lastUserId;51 }52 53 @Override54 public void deleteUser(int id) throws Exception {55 SqlSession sqlSession = sqlSessionFactory.openSession();56 sqlSession.delete("test.deleteUser", id);57 sqlSession.commit();58 sqlSession.close();59 }60 61 @Override62 public void updateUser(User user) throws Exception {63 SqlSession sqlSession = sqlSessionFactory.openSession();64 sqlSession.update("test.updateUser", user);65 sqlSession.commit();66 sqlSession.close();67 }68 69 }
测试:
1 package com.test.mybatis001; 2 3 4 import java.io.InputStream; 5 import java.util.List; 6 7 import org.apache.ibatis.io.Resources; 8 import org.apache.ibatis.session.SqlSessionFactory; 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder;10 import org.junit.Before;11 import org.junit.Test;12 13 import com.mybatis001.bean.User;14 import com.mybatis001.dao.UserDao;15 import com.mybatis001.dao.impl.UserDaoImpl;16 17 public class UserDaoImplTest {18 19 // 会话工厂20 private SqlSessionFactory sqlSessionFactory;21 22 @Before23 public void setUp() throws Exception {24 // 加载配置文件25 String resource = "SqlMapConfig.xml";26 InputStream inputStream = Resources.getResourceAsStream(resource);27 28 // 根据mytais的配置创建SqlSessionFactory29 sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);30 }31 32 @Test33 public void testFindUserById() throws Exception {34 UserDao userDao = new UserDaoImpl(sqlSessionFactory);35 User user = userDao.findUserById(1001);36 // System.out.println(user.toString());37 38 ListuserList = userDao.findUserList("lee");39 40 if (!userList.isEmpty()) {41 for (User u : userList) {42 System.out.println(u.toString());43 }44 }45 }46 47 @Test48 public void insertUser() throws Exception {49 UserDao userDao = new UserDaoImpl(sqlSessionFactory);50 51 User u1 = new User("nathan.lee.salvatore", 18, "男");52 User u2 = new User("nathan", 22, "男");53 User u3 = new User("leechenxiang", 20, "男");54 User u4 = new User("李晨翔", 19, "男");55 56 userDao.insertUser(u1);57 userDao.insertUser(u2);58 userDao.insertUser(u3);59 userDao.insertUser(u4);60 }61 62 @Test63 public void deleteUser() throws Exception {64 UserDao userDao = new UserDaoImpl(sqlSessionFactory);65 userDao.deleteUser(1013);66 }67 68 @Test69 public void updateUser() throws Exception {70 UserDao userDao = new UserDaoImpl(sqlSessionFactory);71 User user = new User("李斯涵", 1, "女");72 user.setId(1001);73 userDao.updateUser(user);74 }75 }
最后附上github地址: