Mybatis实现数据的增删改查

Mybatis实现数据的增删改查

1、项目结构(使用maven创建项目)

Mybatis实现数据的增删改查

2、App.java

 package com.GetcharZp.MyBatisStudy;

 import java.io.IOException;
import java.io.InputStream;
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 com.GetcharZp.MyBatisStudy.dao.TradeDao;
import com.GetcharZp.MyBatisStudy.model.Trade; /**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
InputStream config = null;
try {
config = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(config);
SqlSession session = factory.openSession();
TradeDao tradeDao = session.getMapper(TradeDao.class); /*删除*/
tradeDao.deleteTrade(3);
session.commit(); // 删除要提交 /*查询*/
List<Trade> tradeList = tradeDao.selectTrade();
System.out.println(tradeList);
System.out.println(tradeList.size());
Trade trade = tradeDao.findById(2);
System.out.println(trade); // 日志里面有输出,但结果没有 /*插入*/
Trade InsertTrade = new Trade();
InsertTrade.setTradeId(4);
InsertTrade.setTradeName("毛笔");
InsertTrade.setTradePrice(5.6);
tradeDao.insertTrade(InsertTrade);
session.commit(); /*更新*/
Trade updateTrade1 = new Trade();
updateTrade1.setTradeName("watch");
updateTrade1.setTradePrice(520.1314);
tradeDao.updateTrade(3);
session.commit(); session.close(); } catch (IOException e) {
e.printStackTrace();
} finally {
if (config != null)
try {
config.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

3、TradeDao

 package com.GetcharZp.MyBatisStudy.dao;

 import java.util.List;

 import com.GetcharZp.MyBatisStudy.model.Trade;

 public interface TradeDao { // interface 接口

     public List<Trade> selectTrade();
public void deleteTrade(int tradeId);
public void updateTrade(int tradeId);
public void insertTrade(Trade newTrade);
public Trade findById(int tradeId);
}

4、Trade

 package com.GetcharZp.MyBatisStudy.model;

 public class Trade {

     private Integer tradeId;
private String tradeName;
private Double tradePrice;
public Integer getTradeId() {
return tradeId;
}
public void setTradeId(Integer tradeId) {
this.tradeId = tradeId;
}
public String getTradeName() {
return tradeName;
}
public void setTradeName(String tradeName) {
this.tradeName = tradeName;
}
public Double getTradePrice() {
return tradePrice;
}
public void setTradePrice(Double tradePrice) {
this.tradePrice = tradePrice;
}
@Override
public String toString() {
return "Trade [tradeId=" + tradeId + ", tradeName=" + tradeName + ", tradePrice=" + tradePrice + "]";
}
}

5、TradeMapper

 <?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="com.GetcharZp.MyBatisStudy.dao.TradeDao">
<select id="selectTrade" resultType="com.GetcharZp.MyBatisStudy.model.Trade">
select * from trade
</select>
<select id="findById" resultType="com.GetcharZp.MyBatisStudy.model.Trade">
select * from trade where trade_id=#{tradeId}
</select>
<delete id="deleteTrade">
delete from trade where trade_id = #{tradeId}
</delete>
<insert id="insertTrade">
insert into trade (trade_id, trade_name, trade_price)
values(#{tradeId}, #{tradeName}, #{tradePrice})
</insert>
<update id="updateTrade">
update trade set
trade_name = #{tradeName},
trade_price = #{tradePrice}
where
trade_id = #{tradeId}
</update>
</mapper>

6、mybatis-config.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>
<settings>
<!-- <setting name="logImpl" value="STDOUT_LOGGING" /> -->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<typeAliases>
<package name="com.GetcharZp.MyBatisStudy.model"/>
</typeAliases>
<environments default="dev">
<environment id="dev">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/suse"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mappers/TradeMapper.xml"/>
</mappers>
</configuration>

7、pom.xml:

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.GetcharZp.MyBatisStudy</groupId>
<artifactId>Mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging> <name>Mybatis</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
</dependencies>
</project>
上一篇:微软 WP 新策略:非开发者账号免费解锁 1 部手机(转)


下一篇:php的借用其他网站的页面覆盖Logo的技巧