mybatis 基础配置测试

配置UserMapper.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="User">
<insert id="insertUser"> insert into user2(username,password,type1,age) values('ee','333','ccccc',111)</insert>
<select id="findAll" resultMap="xx">
select  * from user2
</select>
<resultMap type="pojo.User" id="xx">
<result property="type" column="type1"/>
</resultMap>
<delete id="deleteUser">delete from user2 where age=12</delete>
<update id="updateUser"> update user2 set username='小黑' where age=21</update>
<select id="selectO" resultMap="xx">select * from user2 where age=21</select>

</mapper>

配置MappConfig.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>
<!-- 配置数据源 -->
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/spring?characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
<environment id="oracle">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:XE" />
<property name="username" value="ht1602" />
<property name="password" value="htdb" />
</dataSource>
</environment>
</environments>
<!-- 映射文件 -->
<mappers>
<mapper resource="pojo/UserMapper.xml" />
</mappers>

</configuration>

测试test

public class UserTest {


@Test
public void select() throws IOException{
InputStream inputStream=Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session=factory.openSession();
List<User>list=session.selectList("User.findAll");
System.out.println(list);
}
@Test
public void insert() throws IOException{
InputStream inputStream=Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session=factory.openSession();
session.insert("User.insertUser");
session.commit();
}
@Test
public void delete() throws IOException{
InputStream inputStream=Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session=factory.openSession();
session.delete("User.deleteUser");

session.commit();
}

@Test
public void update() throws IOException{

InputStream inputStream=Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session=factory.openSession();
session.update("User.updateUser");
session.commit();
}
@Test
public void selectO() throws IOException{
InputStream inputStream=Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session=factory.openSession();
User u=session.selectOne("User.selectO");
System.out.println(u);
}

阅读更多
个人分类: mybatis
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭