mybatis 入门案例 增删改查
1 导入相关jar包
< dependency>
< groupId> mysql< / groupId>
< artifactId> mysql- connector- java< / artifactId>
< version> 5.1 .6 < / version>
< / dependency>
< dependency>
< groupId> junit< / groupId>
< artifactId> junit< / artifactId>
< version> 4.12 < / version>
< / dependency>
< dependency>
< groupId> org. mybatis< / groupId>
< artifactId> mybatis< / artifactId>
< version> 3.5 .6 < / version>
< / dependency>
2.创建持久化类
package com. hopu. dao ;
public class Account {
private String name;
private double money;
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public double getMoney ( ) {
return money;
}
public void setMoney ( double money) {
this . money = money;
}
@Override
public String toString ( ) {
return "Account [name=" + name + ", money=" + money + "]" ;
}
}
3.创建映射文件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 = " AccountMapper" >
< select id = " findall" resultType = " com.hopu.dao.Account" >
select * from account
</ select>
< update id = " update" parameterType = " com.hopu.dao.Account" >
update account set money = #{money} where name = #{name}
</ update>
< update id = " delete" parameterType = " com.hopu.dao.Account" >
delete from account where name= #{name}
</ update>
< update id = " inster" parameterType = " com.hopu.dao.Account" >
insert into account (name,money) values(#{name},#{money})
</ update>
</ mapper>
4创建jdbc配置文件
driverClass= com. mysql. jdbc. Driver
url= jdbc: mysql: / / localhost: 3306 / test
username= root
password= root
4.创建 MyBatis 的配置文件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>
< properties resource = " jdbc.properties" />
< environments default = " development" >
< environment id = " development" >
< transactionManager type = " jdbc" > </ transactionManager>
< dataSource type = " POOLED" >
< property name = " driver" value = " ${jdbc.driver}" />
< property name = " url" value = " ${jdbc.url}" />
< property name = " username" value = " ${jdbc.username}" />
< property name = " password" value = " ${jdbc.password}" />
</ dataSource>
</ environment>
</ environments>
< mappers>
< mapper resource = " app.xml" > </ mapper>
</ mappers>
</ configuration>
5.创建测试类
package test ;
import java. io. IOException ;
import java. io. InputStream ;
import java. sql. Connection ;
import java. sql. DriverManager ;
import java. sql. PreparedStatement ;
import java. util. List ;
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 com. hopu. dao. Account ;
public class test {
private SqlSession Setting ( ) throws IOException {
InputStream a = Resources . getResourceAsStream ( "mybites.xml" ) ;
SqlSessionFactory sql = new SqlSessionFactoryBuilder ( ) . build ( a) ;
SqlSession aa = sql. openSession ( ) ;
return aa;
}
@Test
public void mybatis ( ) throws IOException {
SqlSession aa = Setting ( ) ;
List < Object > s = aa. selectList ( "AccountMapper.findall" ) ;
System . out. println ( s) ;
}
@Test
public void update ( ) throws Exception {
SqlSession aa = Setting ( ) ;
Account updatemu = new Account ( ) ;
updatemu. setMoney ( 100.0 ) ;
updatemu. setName ( "张三" ) ;
aa. update ( "AccountMapper.update" , updatemu) ;
aa. commit ( ) ;
aa. close ( ) ;
}
@Test
public void delete ( ) throws Exception {
SqlSession aa = Setting ( ) ;
Account updatemu = new Account ( ) ;
updatemu. setName ( "张三" ) ;
aa. delete ( "AccountMapper.delete" , updatemu) ;
aa. commit ( ) ;
aa. close ( ) ;
}
@Test
public void inster ( ) throws Exception {
SqlSession aa = Setting ( ) ;
Account updatemu = new Account ( ) ;
String name= "王五1" ;
double money= 1000.0 ;
updatemu. setMoney ( money) ;
updatemu. setName ( name) ;
aa. insert ( "AccountMapper.inster" , updatemu) ;
aa. commit ( ) ;
aa. close ( ) ;
}
}