HelloWorld.java
package use.basic;
import java.io.IOException;
import java.io.Reader;
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.mison.mapper.NewsMapper;
import com.mison.model.News;
public class HelloWorld {
public static void main(String[] args) {
try {
String resource = "Configuration.xml";
//读取配置文件Configuration.xml
Reader reader = Resources.getResourceAsReader(resource);
SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);
sqlMapper.getConfiguration().addMapper(NewsMapper.class);
SqlSession session = sqlMapper.openSession();
//查询 方式一
News news = (News) session.selectOne("NewsMapper.selectNews", 1);
System.out.println(news.getTitle());
//查询 方式二
NewsMapper mapper = session.getMapper(NewsMapper.class);
News news1 = mapper.selectNews(2);
System.out.println(news1.getAuthor());
News news2 = new News();
news2.setTitle("saf");
news2.setAuthor("hjq");
//插入 方式一
mapper.insertNews(news2);
//插入 方式二
session.insert("NewsMapper.insertNews", news2);
session.commit();
news2.setId(1);
news2.setTitle("123");
session.update("NewsMapper.updateNews", news2);
session.commit();
session.delete("NewsMapper.deleteNews", 6);
session.commit();
session.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
NewsMapper.java
package com.mison.mapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.mison.model.News;
public interface NewsMapper {
@Select("SELECT * FROM news WHERE id = #{id}")
News selectNews(int id);
@Insert("insert into news(id,title,author) values(#{id},#{title},#{author})")
void insertNews(News news);
@Update("update news set title = #{title}, author = #{author} where id = #{id}")
void updateNews(int id);
@Delete("delete from news where id = #{id}")
void deleteNews(int id);
}
News.java
package com.mison.model;
public class News {
private Integer id;
private String title;
private String author;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
Configuration.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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="NewsMapper.xml" />
</mappers>
</configuration>
或
<?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" />
<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="NewsMapper.xml" />
</mappers>
</configuration>
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/test
jdbc.username=root
jdbc.password=root
NewsMapper.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="NewsMapper">
<select id="selectNews" parameterType="int" resultType="com.mison.model.News">
select * from news where id = #{id}
</select>
<insert id="insertNews" parameterType="com.mison.model.News">
insert into news(title,author) values(#{title},#{author})
</insert>
<update id="updateNews" parameterType="com.mison.model.News">
update news set title = #{title} , author = #{author} where id = #{id}
</update>
<delete id="deleteNews" parameterType="com.mison.model.News">
delete from news where id = #{id}
</delete>
</mapper>
- 大小: 20.9 KB
分享到:
相关推荐
MyBatis经典入门实例,适合新手学习用。
mybatis入门实例,即使你刚入门也可以迅速学会!!
Mybatis入门项目实例,笔记,内含单独mybatis jar包
mybatis,入门实例,对单表的CRUD
首先把db文件夹下的数据库文件导入到MySQL数据库中,修改mybatis-cfg.xml文件中的username和password,运行即可(如果是其他数据库,则需要修改driver和url,还需要添加对应的数据库jar包)
mybatis deom.mybatis 的简单入门实例。基于maven开发的。
MyBatis入门实例源码
【Spring+MyBatis+MySQL实战入门】一、MyBatis操作入门的源码
mybatis入门实例(xml)
Spring mvc、 Spring、 Mybatis、 注解实例、 项目实例 从入门到项目实战
mybatis基于maven的简单的入门的实例
Spring mvc + Spring + Mybatis + 注解实例 + 项目实例 从入门到项目实战
springboot+springMVC+mybatis的简单入门实例2,实现了数据自动批量生成和批量插入,使用方便,直接导入运行即可,有现成的数据库脚本
spring3.0mvc自学教程ppt+注解教程+注解实例+springmybatis教程+项目实例+中文api 初学(自学)着的不二法定,从入门到项目实例深入学习(浅-深)
mybatis入门实例(多对一xml)
一个简单springmvc+mybatis实例,算是ssm小型框架吧表需要自己建,然后导入项目里即可,希望能帮助大家
针对零基础用户 入门用户学习mybatis的学习实例
一个简单的Mybatis框架应用实例,使用MyEclipse10+MySqL数据库开发。