初步了解mybatis
初步了解MyBatis
需要导入 mybatis-x.x.x.jar;
连接数据库为mysql 导入对应数据库的 mysql-connector-java-5.1.18-bin.jar
数据库表 t_user 里面字段:id(主键、自增) name passwod age qq
Model层里面对应的类:User
package nie.wei.qing.model;
public class User {
private int age;
private int id;
private String name;
private String password;
private String qq;
//补充getter and setter
}
Dao层,一个对数据操作的接口: UserDao
package nie.wei.qing.dao;
import java.util.List;
import nie.wei.qing.model.User;
public interface UserDao {
public User selectById(int id); //按id查找用户
public List<User> findAll(); //查找所有用户
public void update(User user); //更新用户
public void insert(User user); //插入新的用户
public void deleteById(int id); //按照id来删除对应的用户
}
配置文件 mybatis-congig.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="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/nie" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<mappers>
<!-- 对应的路径 -->
<mapper resource="MybatisUserDao.xml" />
</mappers>
</configuration>
对MySql操作的配置 MyBaitsUserDao.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="nie.wei.qing.dao.UserDao">
<select id="selectById" parameterType="int" resultType="nie.wei.qing.model.User"> <!-- 根据用户id查找用户 -->
select * from t_user where id=#{id}
</select>
<select id="findAll" resultType="nie.wei.qing.model.User"> <!-- 查找所有用户 -->
select * from t_user
</select>
<update id="update" parameterType="nie.wei.qing.model.User"> <!-- 更新库中的用户 -->
update t_user set
name=#{name},password=#{password},age=#{age},qq=#{qq} where id=#{id}
</update>
<insert id="insert" parameterType="nie.wei.qing.model.User"> <!-- 向数据库中插入用户 -->
insert into t_user(name,password,age,qq)
values(#{name},#{password},#{age},#{qq})
</insert>
<delete id="deleteById" parameterType="int"> <!-- 删除用户 -->
delete from t_user where id=#{id}
</delete>
</mapper>
Junit测试代码:
String resource = "mybatis-config.xml"; //mybatis配置文件的路径
Reader reader = Resources.getResourceAsReader(resource);
SqlSessionFactoryBuilder builfer = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builfer.build(reader);
SqlSession session = factory.openSession();
//前面的操作是得到对应的session 有点像hibernate的操作;
//根据配置文件得到UserDao的一个实例
UserDao userDao = session.getMapper(UserDao.class);
//查找对应的id来得到一个user
User user=userDao.selectById(1);
System.out.println(user.getName());
//得到所有的用户
List<User> users=userDao.findAll();
for(int i=0;i<users.size();i++){
System.out.println("id号是:"+users.get(i).getId()+" name is :"+users.get(i).getName());
}
user.setPassword("99999");
userDao.update(user);//更新相应的User
User u=new User();
u.setName("h");
u.setPassword("d");
u.setAge(23);
u.setQq("1111");
userDao.insert(u);//插入新用户
userDao.deleteById(4);//根据id删除对应的用户
session.commit();
session.close();