Mybatis是基于java的持久层框架,它内部封装了jdbc,使开发者只需要关注sql语句本身
通过xml或注解的方式将要执行的各种 statement配置起来,并通过java对象和statement中sql的动态参数进行映射生成最终执行的sql语句。
最后mybatis框架执行sql并将结果映射为java对象并返回。
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
public class User {
private int id;
private String username;
private String password;
//省略set、get、tostring方法
}
<?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">
<!--namespace:命名空间,和下面id一起组成查询标识-->
<mapper namespace="userMapper">
<!--查询所有-->
<!--resultType:查询结果对应的实体类型-->
<select id="findAll" resultType="user">
select * from user
</select>
<!--插入操作-->
<!--parameterType属性指定要插入的数据类型,#{实体属性名}方式引用实体中的属性值-->
<insert id="save" parameterType="user">
insert into user values(#{id},#{username},#{password});
</insert>
<!--修改操作-->
<update id="update" parameterType="user">
update user set username=#{username},password=#{password} where id = #{id};
</update>
<!--删除操作-->
<delete id="delete" parameterType="int">
delete from user where id = #{id};
</delete>
</mapper>
<?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>
<!--加载外部properties文件-->
<properties resource="jdbc.properties"></properties>
<!--配置typeAliases,为cn.guixinchn.domain.User定义别名为: user-->
<typeAliases>
<typeAlias type="cn.guixinchn.domain.User" alias="user"/>
</typeAliases>
<!--数据源环境-->
<environments default="developement">
<environment id="developement">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="{jdbc.driver}"/>
<property name="url" value="{jdbc.url}"/>
<property name="username" value="{jdbc.username}"/>
<property name="password" value="{jdbc.password}"/>
</dataSource>
</environment>
</environments>
<!--加载映射文件-->
<mappers>
<mapper resource="cn/guixinchn/mapper/UserMapper.xml"></mapper>
</mappers>
</configuration>
jdbc.properties
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/test
jdbc.username = root
jdbc.password = 123456
@Test
public void test1() throws IOException {
//模拟数据
User user = new User();
user.setUsername("测试");
user.setPassword("123");
// 获取核心配置文件
InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
// 获取session工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
//获得session回话对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//执行操作 参数: namespace+id
//查询所有
//List<User> userList = sqlSession.selectList("userMapper.findAll");
//打印数据
//System.out.println(userList);
//插入操作
//sqlSession.insert("userMapper.save",user);
//提交数据
//sqlSession.commit();
//修改操作
//sqlSession.update("userMapper.update",user);
//sqlSession.commit();
//删除操作
//sqlSession.delete("userMapper.delete",4);
//sqlSession.commit();
//释放资源
sqlSession.close();
}