1. 程式人生 > >mybatis連接mysql數據庫對數據增,刪,改以及查詢所有

mybatis連接mysql數據庫對數據增,刪,改以及查詢所有

mysql- ret get commit 5.1 row map version resource

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>
 


 







mybatis連接mysql數據庫對數據增,刪,改以及查詢所有