如今的持久化框架中,Hibernate和mybatis可以说是各自占据了半壁江山,所以我们不能单独只了解Hibernate,还得学习mybatis,今天就首先来入门mybatis
要使用mybatis只需下载mybatis然后将mybatis-3.0.4.jar导入到工程中,要使用mybatis就必须先了解他的配置文件,在这里我们来看看mybatis的核心配置文件,名字可以随意命名,configuration,xml
在这里我们定义它的environments,在这个节点里面我们可以配置多个environment,这个使用多个环境的时候就可以用到,随后就是定义他的dataSource,就是一般的驱动,数据库,用户名和密码,配置完之后,我们还需要配置映射文件,如我们这里有个userDao-mapping.xml
接着就是这个userDao-mapping.xml
内容如下:
可以看到,首先配置了mapper,虽然现在不知道是什么鬼,但是可以看到就是一个执行数据库操作的一个接口,然后可以看到定义了一个查询 resultType是一个实体类
在这里需要注意的是这个查询的id需要和接口中的方法名一致,还有这样配置的缺点是数据库列名必须和实体名保持一致,要想自定义的话我们可以在上面定义一个resultMap
这样配置文件就变成了如下:
我们的实体类为:
package main.java.com.mybatis; public class User { private int id ; private String userName ; private String userType ; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserType() { return userType; } public void setUserType(String userType) { this.userType = userType; } @Override public String toString() { return "User [id=" + id + ", userName=" + userName + ", userType=" + userType + "]"; } }
定义好了配置文件,我们就需要来写一个数据库执行类,UserDao
package main.java.com.mybatis; import java.util.List; public interface UserDao { public void insert(User user); public User findUserById(int userId); public ListfindAllUsers(); }
可以看到这就是一个接口,然后定义了一些方法。
最后来测试一下吧
package main.java; import java.io.IOException; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import main.java.com.mybatis.User; import main.java.com.mybatis.UserDao; import main.java.com.mybatis.UserDaoImp; public class Main { public static void main(String[] args) { SqlSession sqlSession = getSessionFactory().openSession(); UserDao userMapper = sqlSession.getMapper(UserDao.class); User user = userMapper.findUserById(2); System.out.println(user); } private static SqlSessionFactory getSessionFactory() { SqlSessionFactory sessionFactory = null; String resource = "main/java/conf/configuration.xml"; try { sessionFactory = new SqlSessionFactoryBuilder().build(Resources .getResourceAsReader(resource)); } catch (IOException e) { e.printStackTrace(); } return sessionFactory; } }
输出为:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/JettyProject/SpringStudyPro/WebContent/WEB-INF/lib/activemq-all-5.14.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/JettyProject/SpringStudyPro/WebContent/WEB-INF/lib/slf4j-jdk14-1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
User [id=2, userName=dss, userType=dsa]
这就是一个简单的mybatis的实用例子,很简单吧
下面贴出使用mybatis实现增删改查的例子:
先贴出mapping配置文件:
userDao-mapping.xml
insert into user_t(user_name , user_type) values (#{userName} , #{userType}) delete from user_t where id=#{id} update user_t set user_name=#{userName},user_type=#{userType} where id=#{id} insert user_t(user_name , user_type) values (#{userName} , #{userType})
实现类:
userDaoImp
package main.java.com.mybatis; import java.io.IOException; import java.util.List; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class UserDaoImp { public void updateUserById(){ User user = findUserById(3); user.setUserName("updateDengwei"); SqlSession sqlSession = getSessionFactory().openSession(); sqlSession.update("updateUser", user); sqlSession.commit(); } public void deleteUserById(int id){ SqlSession sqlSession = getSessionFactory().openSession() ; sqlSession.delete("deleteUserById", id); sqlSession.commit(); } public void insertUser(User user){ SqlSession sqlSession = getSessionFactory().openSession(); UserDao userMapper = sqlSession.getMapper(UserDao.class); sqlSession.insert("insertUser", user); sqlSession.commit(); System.out.println("OK"); } public User findUserById(int userId) { SqlSession sqlSession = getSessionFactory().openSession(); UserDao userMapper = sqlSession.getMapper(UserDao.class); User user = userMapper.findUserById(userId); System.out.println(user); return user; } public ListfindUserByName(String name){ SqlSession session = getSessionFactory().openSession() ; UserDao userMappper = session.getMapper(UserDao.class); List list = userMappper.findUserByName(name); return list ; } public List findAllUsers() { SqlSession sqlSession = getSessionFactory().openSession(); UserDao userMapper = sqlSession.getMapper(UserDao.class); List users = userMapper.findAllUsers(); for(User user : users){ System.out.println(user); } return null; } private static SqlSessionFactory getSessionFactory() { SqlSessionFactory sessionFactory = null; String resource = "main/java/conf/configuration.xml"; try { sessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader(resource)); } catch (IOException e) { e.printStackTrace(); } return sessionFactory; } }