赞
踩
实验步骤:
一、数据准备
1、建立数据库表(如:UserInfo:id(int)、name(varchar)、age(int))
2、增加一条数据(如:insert UserInfo values(1,"tom",23)
二、建立访问MyBatis的对象
3、建立类型UserInfo(在po包内,与表的名称对应)
- package cn.com.mybatis.po;
-
- import java.io.Serializable;
- import java.util.Date;
- // 持久化实体类
- public class UserInfo {
- private int id;
- private String name;
- private int age;
-
- public UserInfo(){
-
- }
-
- public UserInfo(int id, String name, int age) {
- this.id = id;
- this.name = name;
- this.age = age;
- }
-
- 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 int getAge() {
- return age;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
- }
三、配置MyBatis
4、创建UserInfoMapper.xml(在目录里config/sqlmap/)
- <?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="test">
-
-
-
- </mapper>
5、在UserInfoMapper.xml中增加查询描述(如下所示)
<select id="findUserById" parameterType="int" resultType="cn.com.mybatis.po.User">
SELECT * FROM USER WHERE id=#{id}
</select>
对以上标签内容进行适当修改。
- <?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="test">
-
-
- <select id="findUserInfoById" parameterType="int" resultType="cn.com.mybatis.po.UserInfo">
- SELECT * FROM USER WHERE id=#{id}
- </select>
-
- </mapper>
6、在SqlMapConfig.xml中mappers标签中增加新的Mapper
<mapper resource="sqlmap/UserInfoMapper.xml"/>
- <?xml version="1.0" encoding="UTF-8"?>
- <!--引入DTD,检验xml文件是否符合约定好的某种规范。-->
- <!DOCTYPE configuration
- PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-config.dtd">
- <!--配置信息正文-->
- <configuration>
- <!-- 该标签对中配置日志输出模式logImpl为LOG4J-->
- <settings>
- <setting name="logImpl" value="LOG4J" />
- </settings>
- <objectFactory type="cn.com.mybatis.test.CartObjectFactory"/>
- <plugins>
- <plugin interceptor="cn.com.mybatis.test.QueryPlugin">
- <property name="someProperty" value="100"/>
- </plugin>
- </plugins>
- <!-- 和spring整合后 environments配置将废除-->
- <!-- enviroment标签中配置Mybatis的环境信息-->
- <environments default="development">
- <environment id="development">
- <!-- 使用jdbc事务管理-->
- <transactionManager type="JDBC" />
- <!-- 数据库连接池-->
- <dataSource type="POOLED">
- <property name="driver" value="org.gjt.mm.mysql.Driver"/>
- <property name="url" value="jdbc:mysql://localhost:3306/mybatis_test?characterEncoding=utf-8"/>
- <property name="username" value="root"/>
- <property name="password" value="lele20011019"/>
- </dataSource>
- </environment>
- </environments>
- <mappers>
- <mapper resource="sqlmap/UserMapper.xml"/>
- <mapper resource="sqlmap/CustomerMapper.xml"/>
- <mapper resource="sqlmap/UserInfoMapper.xml"/>
- </mappers>
- </configuration>
四、测试
7、创建测试类MyFirstTest(在cn.com.mybatis.test包中)
8、在MyFirstTest类中创建TestSelectUserInfo方法
9、成功运行TestSelectUserInfo方法
- public class MyFirstTest {
- public DataConnection dataConn = new DataConnection();
-
- @Test
- public void TestSelectUserInfo() throws IOException {
- SqlSession sqlSession=dataConn.getSqlSession();
- UserInfo userInfo=sqlSession.selectOne("test.findUserInfoById",1);
- System.out.println("id:"+userInfo.getId());
- System.out.println("name:"+userInfo.getName());
- System.out.println("age:"+userInfo.getAge());
- sqlSession.close();
- }
- }
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。