1. 程式人生 > >mybatis資料庫的基本操作

mybatis資料庫的基本操作

        昨天的程式碼中,在成功編輯後,我們是無法看到裡面資料的具體操作,解決方案則是在原來基礎的加上一個log4j.properties(檔名不能錯,這是一個坑,否則無法看到具體操作,首先先看資料夾各個層級。(本次筆記尚未使用別名進行操作,其中java.lang.Long可以更改為long的別名)。

    

    

然後資料庫各個分層次程式碼。

更新語句

查詢一個物件的語句

查詢所有物件的語句

刪除物件的具體操作

具體程式碼(mybatieutils為一個方法)

mybatis.xml檔案程式碼

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE configuration

PUBLIC

<configuration>

<environments default="default">

<environment id="default">

<transactionManager type="JDBC"></transactionManager>

<dataSource type="POOLED">

<property

name="driver" value="com.mysql.jdbc.Driver"></property>

<property name="username" value="root"></property>

<property name="password" value="123456"></property>

</dataSource>

</environment>

</environments>

<mappers>

<mapper resource="mybatis/domain/usermapper.xml"

></mapper>      //關聯其他xml檔案(執行對user類的物件操作)

</mappers>

</configuration>

usermapper.xml檔案程式碼

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE mapper

PUBLIC

<mapper namespace="mybatis.domain.usermapper">

<insert id="save" parameterType="mybatis.domain.user" keyColumn="id" keyProperty="id" useGeneratedKeys="true">

INSERT INTO user (username,password,age)VALUES (#{username},#{password},#{age})

</insert>

<update id="update" parameterType="mybatis.domain.user">

UPDATE user SET username=#{username},password=#{password},age=#{age} WHERE id=#{id}

</update>

<select id="search" parameterType="java.lang.Long" resultType="mybatis.domain.user">      //java.lang.Long可以更換為long

SELECT *FROM user WHERE id=#{id}

</select>

<select id="list" resultType="mybatis.domain.user">

SELECT *FROM user

</select>

<delete id="delete" parameterType="java.lang.Long">

DELETE FROM user WHERE id=#{id}

</delete>

</mapper>

mybatisutils工具類的編寫

package mybatis.utils;

import org.apache.ibatis.io.Resources;

import org.apache.ibatis.session.SqlSession;

import org.apache.ibatis.session.SqlSessionFactory;

import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;

public class mybatisutils {

private SqlSessionFactory sf;

private static mybatisutils instance=new mybatisutils();

public mybatisutils(){

try {

sf=new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis.xml"));

} catch (IOException e) {

e.printStackTrace();

}

}

public static SqlSession opensession(){

return instance.sf.openSession();            //靜態方法不能直接呼叫非靜態方法。

}

}

log4j.properties(注意檔名千萬不能有錯)

# Global logging configuration

log4j.rootLogger=ERROR, stdout

# MyBatis logging configuration...

log4j.logger.mybatis.domain.usermapper=TRACE     //mybatis.domain.usermapper為具體操作資料庫的那個xml所在的路徑

# Console output...

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n  

user類

package mybatis.domain;

import lombok.Data;

public class user {

private Long id;

private String username;

private String password;

private Integer age;

public Long getId() {

return id;

}

public void setId(Long id) {

this.id = id;

}

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public Integer getAge() {

return age;

}

public void setAge(Integer age) {

this.age = age;

}

@Override

public String toString() {

return "user{" +

"id=" + id +

", username='" + username + '\'' +

", password='" + password + '\'' +

", age=" + age +

'}';

}

}

usertest測試類

package mybatis.test;

import mybatis.domain.user;

import mybatis.utils.mybatisutils;

import org.apache.ibatis.io.Resources;

import org.apache.ibatis.session.SqlSession;

import org.apache.ibatis.session.SqlSessionFactory;

import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import org.junit.Test;

import java.io.IOException;

import java.util.List;

public class usertest {

@Test

public void test() throws IOException {

user u=new user();

u.setAge(17);

u.setUsername("小李子");

u.setPassword("123456");

//SqlSessionFactory sf=new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis.xml"));

SqlSession session=mybatisutils.opensession();

session.insert("mybatis.domain.usermapper.save",u);

session.close();

System.out.println(u);

}

@Test

public void testupdate(){

user u=new user();

u.setAge(47);

u.setUsername("李公公");

u.setPassword("123");

u.setId(1L);

SqlSession session=mybatisutils.opensession();

session.update("mybatis.domain.usermapper.update",u);

session.close();

}

@Test

public void testget(){

SqlSession session=mybatisutils.opensession();

user u=session.selectOne("mybatis.domain.usermapper.search",1L);

System.out.println(u);

session.close();

}

@Test

public void testlist(){

SqlSession session=mybatisutils.opensession();

List<user>users=session.selectList("mybatis.domain.usermapper.list");

for (user user : users) {

System.out.println(user);

}

session.close();

}

@Test

public void testdelete(){

SqlSession session=mybatisutils.opensession();

session.delete("mybatis.domain.usermapper.delete",7L);

session.close();

}

}

測試結果如下圖