Mybatis之入门实例
- MyBatis 是什么
MyBatis是一款一流的支持自定义 SQL、存储过程和高级映射的持久化框架。MyBatis几乎消除了所有的JDBC代码,也基本不需要手工去设置参数和获取检索结果。MyBatis能够使用简单的XML格式或者注解进行来配置,能够映射基本数据元素、Map接口和POJOs(普通java 对象)到数据库中的记录。
- MyBatis 开发配置
mybatis-3.1.1.jar
mysql-connector-java-5.0.4-bin.jar
junit-4.8.1.jar(可选:用于测试)
- MyBatis 入门实例
第一个MyBatis项目目录:
开发流程:
1)在src目录下创建mybatis的配置文件mybatis-config.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="jdbc.properties" /> <typeAliases> <typeAlias type="cn.mybatis.model.User" alias="User" /> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="${driver}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> <property name="url" value="${url}" /> </dataSource> </environment> </environments> <mappers> <mapper resource="cn/mybatis/model/User.xml" /> </mappers> </configuration>
username=root password=mysql url=jdbc:mysql://localhost:3306/test driver=com.mysql.jdbc.Driver
2)创建数据库表并生成相应的实体类
DROP TABLE IF EXISTS `t_user`; CREATE TABLE `t_user` ( `id` int(10) NOT NULL auto_increment, `name` varchar(20) default NULL COMMENT '姓名', `nickname` varchar(20) default NULL COMMENT '昵称', `password` varchar(10) default NULL COMMENT '用户密码', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
package cn.mybatis.model; public class User { private int id; private String name; private String nickname; private String password; 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 String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", nickname=" + nickname + ", password=" + password + "]"; } }
3)生成实体类的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="cn.mybatis.model.User"> <insert id="add" parameterType="User"> insert into t_user (name,password,nickname) value (#{name},#{password},#{nickname}) </insert> <update id="update" parameterType="User"> update t_user set name=#{name},nickname=#{nickname},password=#{password} where id=#{id} </update> <delete id="delete" parameterType="int"> delete from t_user where id=#{id} </delete> <select id="load" parameterType="int" resultType="User"> select * from t_user where id=#{id} </select> </mapper>
4)创建获取SQlSession的Util类
package cn.mybatis.util; import java.io.IOException; import java.io.InputStream; 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 MyBatisUtil { private static SqlSessionFactory factory; static { try { InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); factory = new SqlSessionFactoryBuilder().build(is); } catch (IOException e) { e.printStackTrace(); } } public static SqlSession createSession() { return factory.openSession(); } public static void closeSession(SqlSession session) { if (session != null) session.close(); } }
5)Junit方式测试SqlSession完成对数据库的操作
package cn.mybatis.test; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import cn.mybatis.model.User; import cn.mybatis.util.MyBatisUtil; public class MybatisTest01 { @Test public void testAdd() { SqlSession session = null; try { session = MyBatisUtil.createSession(); User u = new User(); u.setName("Tom"); u.setNickname("Tomy"); u.setPassword("123"); session.insert(User.class.getName() + ".add", u); session.commit(); } catch (Exception e) { e.printStackTrace(); session.rollback(); } finally { MyBatisUtil.closeSession(session); } } @Test public void testUpdate() { SqlSession session = null; try { session = MyBatisUtil.createSession(); User u = new User(); u.setId(1); u.setName("Tom"); u.setNickname("Tomy"); u.setPassword("123456"); session.update(User.class.getName() + ".update", u); session.commit(); } catch (Exception e) { e.printStackTrace(); session.rollback(); } finally { MyBatisUtil.closeSession(session); } } @Test public void testSelect() { SqlSession session = null; try { session = MyBatisUtil.createSession(); User u = (User) session.selectOne(User.class.getName() + ".load", 1); System.out.println("name: " + u.getName()); } finally { MyBatisUtil.closeSession(session); } } @Test public void testDelete() { SqlSession session = null; try { session = MyBatisUtil.createSession(); session.delete(User.class.getName() + ".delete", 1); session.commit(); } catch (Exception e) { e.printStackTrace(); session.rollback(); } finally { MyBatisUtil.closeSession(session); } } }
相关推荐
dongxurr 2020-08-08
liqiancao 2020-07-26
chenjiazhu 2020-07-08
xiuyangsong 2020-11-16
Nishinoshou 2020-11-09
jimgreatly 2020-09-01
dongxurr 2020-08-18
Dullonjiang 2020-08-15
Dullonjiang 2020-08-11
Dullonjiang 2020-08-09
yunzhonmghe 2020-08-07
jimgreatly 2020-08-03
Dullonjiang 2020-07-30
jimgreatly 2020-07-27
xiuyangsong 2020-07-26
dongxurr 2020-07-26
mcvsyy 2020-07-26
helloxusir 2020-07-25