mybatis对数据库的数据的操作
实体类
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
dao层的接口
public interface IUserInfoDAO {
/**
* 查询所有
* @return
* @throws Exception
*/
public List<UserInfo> getAll();
/**
* 新增用户
* @param user
* @return
*/
public int addSession(UserInfo user) throws Exception;
/**
* 修改
* @param id
* @return
*/
public int updateUser(int id);
/**
* 删除
* @param id
* @return
*/
public int delUser(int id);
}
xml文件(小配置文件)
<mapper namespace="cn.happy.dao.IUserInfoDAO"> 这个是小配置文件必不可少的
<select id="getAll" parameterType="int" resultType="cn.happy.entity.UserInfo" useCache="true">
select * from userinfo
</select>
<insert id="addSession">
insert into userinfo(name,age) value(#{name},#{age})
</insert>
<update id="updateUser" >
UPDATE userinfo SET NAME=#{name} WHERE id=#{id}
</update>
<delete id="delUser">
delete from userInfo where id=#{id}
</delete>
</mapper>
测试类
/**
* 查询所有
*/
@org.junit.Test
public void getAll(){
SqlSession session = MyBatisUtil.getSession();
IUserInfoDAO mapper = session.getMapper(IUserInfoDAO.class);
List<UserInfo> all = mapper.getAll();
for (UserInfo item : all) {
System.out.println("编号:"+item.getId()+"姓名:"+item.getName()+"年龄:"+item.getAge());
}
session.close();
}
/**
* 删除
*/
@org.junit.Test
public void delUser() throws Exception{
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
//获得会话工厂
SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sf.openSession();
int count = session.delete("cn.happy.dao.IUserInfoDAO.delUser",3);
System.out.println(count);
session.commit();
session.close();
}
/**
* 修改
*/
@org.junit.Test
public void updateUser() throws Exception{
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
//获得会话工厂
SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sf.openSession();
UserInfo userInfo=new UserInfo();
userInfo.setName("Hello");
userInfo.setId(3);
int count = session.update("cn.happy.dao.IUserInfoDAO.updateUser",userInfo);
System.out.println(count);
session.commit();
session.close();
}
/**
* 新增
* @throws Exception
*/
public void inserts() throws Exception{
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
//获得会话工厂
SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sf.openSession();
UserInfo userInfo=new UserInfo();
userInfo.setName("晓华");
userInfo.setAge(25);
int count = session.insert("cn.happy.dao.IUserInfoDAO.addSession",userInfo);
System.out.println(count);
session.commit();
session.close();
}
大配置文件
<configuration>
<environments default="development">
<environment id="development">
<!-- 事务管理方案 -->
<!--事务分类:
编程式事务: xx.commit() 即需要手动编写
配置式事务:
JDBC与MANAGED 区别
-->
<transactionManager type="JDBC" />
<!--
POOLED:连接池
UNPOOLED:非连接池
JNDI:Java Naming ANd Directory Interface java命名和目录接口
-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root" />
<property name="password" value="123156" />
</dataSource>
</environment>
<!--该内容在一个xml中可以有多个-->
<!--<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/test"/>
<property name="username" value="root" />
<property name="password" value="123156" />
</dataSource>
</environment>-->
</environments>
<!--映射文件:描述某个实体和数据库表的对应关系 -->
<mappers>
<mapper resource="cn/happy/dao/IUserInfoDAO.xml" />
</mappers>
</configuration>
即便有了上面的一系列配置,但是也不法访问到数据库
必须要有数据的依赖
<!-- log4-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- mysql数据库 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>