导入 mybaties-3.3.1.jar包
导入mysql-connector-java.jar包
首先mybaties配置文件配置数据连接相关信息
<?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> <!-- 类别名定义 --> <typeAliases> <typeAlias type="com.yjm.entry.TestData" alias="testdata123"/> </typeAliases> <!-- 配置Mybatis的环境,事务及数据源等等 --> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://127.0.0.1:3306/test" /> <property name="username" value="root" /> <property name="password" value="1234" /> </dataSource> </environment> </environments> <!-- 指定映射文件或者映射类 --> <mappers> <mapper resource="com/yjm/mapper/testdatamapper.xml" /> </mappers> </configuration>
编写实体类与实体类的查询接口具体查询映射文件
package com.yjm.entry; public class TestData { private String id; private String name; private String password; /** * @param id * the id to set */ public void setId(String id) { this.id = id; } /** * @return the id */ public String getId() { return id; } /** * @param name * the name to set */ public void setName(String name) { this.name = name; } /** * @return the name */ public String getName() { return name; } /** * @param password * the password to set */ public void setPassword(String password) { this.password = password; } /** * @return the password */ public String getPassword() { return password; } }
package com.yjm.mapper; import com.yjm.entry.TestData; public interface TestDataMapper { public void insertData(TestData testData); // public TestData selectData(String id); }
<?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="com.yjm.mapper.TestDataMapper"> <insert id="insertData" parameterType="testdata123" flushCache="true"> INSERT INTO testdata(id,name,password) values(#{id},#{name},#{password}) </insert> </mapper>
上面一个是实体类查询接口,一个是实体类查询接口的具体实现xml。
使用xml来实现实体类的查询接口查询语句。方便编写
下面是测试
package com.yjm.test; import java.io.InputStream; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.yjm.entry.TestData; import com.yjm.mapper.TestDataMapper; public class TestDa { public static void main(String[] args) { InputStream is = TestDa.class.getResourceAsStream("/mybaties-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); SqlSession sqlsession = sqlSessionFactory.openSession(); TestDataMapper testDataMapper=sqlsession.getMapper(TestDataMapper.class); TestData test = new TestData(); test.setId("id"); test.setName("name"); test.setPassword("password"); testDataMapper.insertData(test); sqlsession.commit(); } }
有一点mybaties没有找到获取事务对象。来设置事务启用是否自动提交
package com.yjm.test;
import java.io.InputStream;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.yjm.entry.TestData;
import com.yjm.mapper.TestDataMapper;
public class TestDa {
public static void main(String[] args) {
InputStream is = TestDa.class.getResourceAsStream("/mybaties-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlsession = sqlSessionFactory.openSession();
TestDataMapper testDataMapper=sqlsession.getMapper(TestDataMapper.class);
TestData test = new TestData();
test.setId("id");
test.setName("name");
test.setPassword("password");
testDataMapper.insertData("sdqf","df","gdg");
sqlsession.commit();
}
}
package com.yjm.mapper;
import org.apache.ibatis.annotations.Param;
import com.yjm.entry.TestData;
public interface TestDataMapper {
public void insertData(@Param("id1")String id ,@Param("name")String name,@Param("password")String password);
//public TestData selectData(String id);
}
需要加@param来传递数据,xml配置文件里只能接受一个参数,所以需要改成map形式的,将String映射成map类型
返回map 报错
public Map<String,TestData> selectData(String password);
Exception in thread "main" org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 5
正确写法
public List<Map<String,TestData>> selectData(String password);
或者 public List<TestData> selectData(String password);
Mybatis返回的Map是这样的一种格式:Map<字段名称,字段值>
一个Map<字段名,字段值>对象代表一行数据
返回多行数据是多个Map,多个Map放在List中只能返回一个参数对象