话不多说,马上开始我们的第一个Mybatis程序;
第一个程序,当然要参考MyBatis的官网文档来搞,地址如下:https://mybatis.org/mybatis-3/zh/getting-started.html;
第一个程序,源代码托管在github,地址如下:https://github.com/gdoujkzz/MybatisDemo 的FirstDemo;
我的第一个MyBatis程序
第一步:先创建Maven工程,引入Mybatis;
第二步:在数据库中创建表和插入数据,脚本如下:
create table Test_User( Id number(10) primary key, Name varchar2(50) not null ) insert into Test_User(id,name) values(1,'张三'); insert into Test_User(id,name) values(2,'李四'); insert into Test_User(id,name) values(3,'王五'); insert into Test_User(id,name) values(4,'赵六');
第三步:创建和数据库对应的POJO类;
public class User implements Serializable { private Integer id; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { final StringBuilder sb = new StringBuilder("User{"); sb.append("id=").append(id); sb.append(", name='").append(name).append('''); sb.append('}'); return sb.toString(); } }
第四步 编写全局的配置文件
<?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> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="oracle.jdbc.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@//www.test.com:1601/mestst9"/> <property name="username" value="mespro"/> <property name="password" value="mesprotst123"/> </dataSource> </environment> </environments> <mappers> <mapper resource="mapper/TestUserMapper.xml"></mapper> </mappers> </configuration>
第五步 编写映射文件
<?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="mapper/TestUserMapper"> <select id="selectUser" resultType="com.gdou.mes.User"> select * from test_user </select> <select id="selectUserById" resultType="com.gdou.mes.User"> select * from test_user where id=#{id} </select> </mapper>
第六步 加载配置文件并开始操作
public class UserMapperTest { private SqlSessionFactory factory; @Before public void SetUp() throws Exception { InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml"); factory = new SqlSessionFactoryBuilder().build(inputStream); } /** * Rigorous Test :-) */ @Test public void selectTest() throws Exception { SqlSession session = factory.openSession(); try { //查询所有数据 System.out.println("--------------查询所有数据开始-------------------"); List<User> list = session.selectList("mapper/TestUserMapper.selectUser"); for (User user : list) { System.out.println(user); } System.out.println("--------------查询所有数据结束-------------------"); //查询单条数据 System.out.println("---------------查询单条数据开始-------------------"); User user = session.selectOne("mapper/TestUserMapper.selectUserById", 1); System.out.println(user); System.out.println("---------------查询单条数据结束-------------------"); } catch (Exception ex) { System.out.println(ex); } finally { session.close(); } } }