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>