当前位置:   article > 正文

【JaveEE】MyBatis入门实验_javaeemybatis实验

javaeemybatis实验

实验步骤:

一、数据准备

1、建立数据库表(如:UserInfo:id(int)、name(varchar)、age(int))

2、增加一条数据(如:insert UserInfo values(1,"tom",23)

 

二、建立访问MyBatis的对象

3、建立类型UserInfo(在po包内,与表的名称对应)

 

  1. package cn.com.mybatis.po;
  2. import java.io.Serializable;
  3. import java.util.Date;
  4. // 持久化实体类
  5. public class UserInfo {
  6. private int id;
  7. private String name;
  8. private int age;
  9. public UserInfo(){
  10. }
  11. public UserInfo(int id, String name, int age) {
  12. this.id = id;
  13. this.name = name;
  14. this.age = age;
  15. }
  16. public int getId() {
  17. return id;
  18. }
  19. public void setId(int id) {
  20. this.id = id;
  21. }
  22. public String getName() {
  23. return name;
  24. }
  25. public void setName(String name) {
  26. this.name = name;
  27. }
  28. public int getAge() {
  29. return age;
  30. }
  31. public void setAge(int age) {
  32. this.age = age;
  33. }
  34. }

三、配置MyBatis

4、创建UserInfoMapper.xml(在目录里config/sqlmap/)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="test">
  6. </mapper>

5、在UserInfoMapper.xml中增加查询描述(如下所示)

<select id="findUserById" parameterType="int" resultType="cn.com.mybatis.po.User">  

      SELECT * FROM USER WHERE id=#{id}  

</select>

对以上标签内容进行适当修改。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="test">
  6. <select id="findUserInfoById" parameterType="int" resultType="cn.com.mybatis.po.UserInfo">
  7. SELECT * FROM USER WHERE id=#{id}
  8. </select>
  9. </mapper>

6、在SqlMapConfig.xml中mappers标签中增加新的Mapper

<mapper resource="sqlmap/UserInfoMapper.xml"/>

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--引入DTD,检验xml文件是否符合约定好的某种规范。-->
  3. <!DOCTYPE configuration
  4. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  5. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  6. <!--配置信息正文-->
  7. <configuration>
  8. <!-- 该标签对中配置日志输出模式logImpl为LOG4J-->
  9. <settings>
  10. <setting name="logImpl" value="LOG4J" />
  11. </settings>
  12. <objectFactory type="cn.com.mybatis.test.CartObjectFactory"/>
  13. <plugins>
  14. <plugin interceptor="cn.com.mybatis.test.QueryPlugin">
  15. <property name="someProperty" value="100"/>
  16. </plugin>
  17. </plugins>
  18. <!-- 和spring整合后 environments配置将废除-->
  19. <!-- enviroment标签中配置Mybatis的环境信息-->
  20. <environments default="development">
  21. <environment id="development">
  22. <!-- 使用jdbc事务管理-->
  23. <transactionManager type="JDBC" />
  24. <!-- 数据库连接池-->
  25. <dataSource type="POOLED">
  26. <property name="driver" value="org.gjt.mm.mysql.Driver"/>
  27. <property name="url" value="jdbc:mysql://localhost:3306/mybatis_test?characterEncoding=utf-8"/>
  28. <property name="username" value="root"/>
  29. <property name="password" value="lele20011019"/>
  30. </dataSource>
  31. </environment>
  32. </environments>
  33. <mappers>
  34. <mapper resource="sqlmap/UserMapper.xml"/>
  35. <mapper resource="sqlmap/CustomerMapper.xml"/>
  36. <mapper resource="sqlmap/UserInfoMapper.xml"/>
  37. </mappers>
  38. </configuration>

四、测试

7、创建测试类MyFirstTest(在cn.com.mybatis.test包中)

8、在MyFirstTest类中创建TestSelectUserInfo方法

9、成功运行TestSelectUserInfo方法

  1. public class MyFirstTest {
  2. public DataConnection dataConn = new DataConnection();
  3. @Test
  4. public void TestSelectUserInfo() throws IOException {
  5. SqlSession sqlSession=dataConn.getSqlSession();
  6. UserInfo userInfo=sqlSession.selectOne("test.findUserInfoById",1);
  7. System.out.println("id:"+userInfo.getId());
  8. System.out.println("name:"+userInfo.getName());
  9. System.out.println("age:"+userInfo.getAge());
  10. sqlSession.close();
  11. }
  12. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/541409
推荐阅读
相关标签
  

闽ICP备14008679号