MyBatis配置(1)
1.先导入jar包
项目分支图
2.创建实体类User对象
public class User {
private int a_id;
private String a_name;
private String a_pwd;
public int getA_id() {
return a_id;
}
public void setA_id(int a_id) {
this.a_id = a_id;
}
public String getA_name() {
return a_name;
}
public void setA_name(String a_name) {
this.a_name = a_name;
}
public String getA_pwd() {
return a_pwd;
}
public void setA_pwd(String a_pwd) {
this.a_pwd = a_pwd;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
}
3.配置mybatis.cfg.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>
<typeAliases>
<typeAlias alias="user" type="com.senchen.entity.User" />
</typeAliases>
<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://localhost:3306/test3" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<mappers>
<!-- 注册UsersMapper.xml文件, usersMapper.xml位于com.senchen.mapper这个包下,所以resource写成com/senchen/mapper/UserMapper.xml -->
<mapper resource="com/senchen/mapper/UsersMapper.xml" />
</mappers>
</configuration>
4.配置实体类的专属ORM文件(UsersMapper.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="com.senchen.mapper.UsersMapper">
<select id="findById" parameterType="int" resultType="com.senchen.entity.User">
select * from admin where a_id=#{id}
</select>
<select id="findAll" resultType="user"><!-- 也可以写成resultType="com.senchen.entity.User" -->
select * from admin
</select>
<insert id="save" parameterType="user"><!--user 也可以写成com.senchen.entity.User 一个意思 -->
insert into admin values(#{a_id},#{a_name},#{a_pwd})
</insert>
</mapper>
5.测试类
package com.senchen.test;
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.senchen.entity.User;
public class Test {
public static void main(String[] args) {
//web项目classpath reqeust.getSession().getServletContext().getRealPath("/")
InputStream is = Test.class.getResourceAsStream("/mybatis.cfg.xml");
//构建sqlSession的工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
//获取一个sqlSession
SqlSession session = sessionFactory.openSession();
//执行查询id值返回一个唯一user对象的sql路径
User uu = session.selectOne("com.senchen.mapper.UsersMapper.findById",2);
System.out.println("查询返回一个唯一user对象"+uu);
//执行查询返回多个user对象集合 的sql路径
List<User> ulist = session.selectList("com.senchen.mapper.UsersMapper.findAll");
//System.out.println("---集合 " + (ulist!=null?ulist.size():-1));
System.out.println("行查询返回多个user对象集合"+ulist);
<!--插入数据 -->
User u = new User();
u.setA_id(4);
u.setA_name("张三");
u.setA_pwd("abc123");
session.insert("com.senchen.mapper.UsersMapper.save",u);
session.commit();
}
}
查询结果:
查询返回一个唯一user对象com.senchen.entity.User@2cba5bdb
查询返回多个user对象集合[com.senchen.entity.User@53343ed0, com.senchen.entity.User@b07f45d, com.senchen.entity.User@373c0b53]