User.java:
-
package com.mybatis.entity;
-
-
public class User {
-
-
/*
-
* ID
-
*/
-
private int id;
-
-
/*
-
* 用户姓名
-
*/
-
private String name;
-
-
/*
-
* age
-
*/
-
private int age;
-
-
public int getId() {
-
return id;
-
}
-
-
public void setId(int id) {
-
this.id = id;
-
}
-
-
public String getName() {
-
return name;
-
}
-
-
public void setName(String name) {
-
this.name = name;
-
}
-
-
public int getAge() {
-
return age;
-
}
-
-
public void setAge(int age) {
-
this.age = age;
-
}
-
-
@Override
-
public String toString() {
-
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
-
}
-
-
}
注解方式的UserMapper.java
-
package com.mybatis.mapper;
-
-
import java.util.List;
-
-
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.mybatis.entity.User;
-
-
public interface UserMapper {
-
@Insert("insert into users(name,age) values(#{name},#{age})")
-
public void save(User user);
-
@Update("update users set name=#{name},age=#{age} where id=#{id}")
-
public boolean update(User user);
-
@Delete("delete from users where id=#{id}")
-
public boolean delete(int id);
-
@Select("select * from users where id=#{id}")
-
public User findById(int id);
-
@Select("select * from users")
-
public List<User> findAll();
-
}
注册这个接口,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>
-
-
<!-- 数据库配置文件 -->
-
<properties resource="db.properties" />
-
-
<!-- 实体类,简称 -设置别名 -->
-
<typeAliases>
-
<typeAlias alias="User" type="com.mybatis.entity.User" />
-
</typeAliases>
-
-
<!-- 读取db.properties里的数据,进行数据库配置 -->
-
<environments default="development">
-
<environment id="development">
-
<transactionManager type="JDBC" />
-
<dataSource type="POOLED">
-
<property name="driver" value="${driver}"/>
-
<property name="url" value="${url}" />
-
<property name="username" value="${username}"/>
-
<property name="password" value="${password}"/>
-
</dataSource>
-
</environment>
-
</environments>
-
-
<!--
-
注册userMapper.xml文件
-
<mappers>
-
<mapper resource="com/mybatis/mapper/userMapper.xml"/>
-
</mappers>
-
-->
-
-
<mappers>
-
<mapper class="com.mybatis.mapper.UserMapper"/>
-
</mappers>
-
-
</configuration>
db.properties:
-
driver=com.mysql.jdbc.Driver
-
url=jdbc\:mysql\://localhost\:3306/db_mysql?useUnicode\=true&characterEncoding\=utf-8
-
username=root
-
password=111
日志文件
-
log4j.properties\uFF0C
-
log4j.rootLogger=DEBUG, Console
-
#Console
-
log4j.appender.Console=org.apache.log4j.ConsoleAppender
-
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
-
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
-
log4j.logger.java.sql.ResultSet=INFO
-
log4j.logger.org.apache=INFO
-
log4j.logger.java.sql.Connection=DEBUG
-
log4j.logger.java.sql.Statement=DEBUG
-
log4j.logger.java.sql.PreparedStatement=DEBUG
进行测试:
-
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.mybatis.entity.User;
-
import com.mybatis.mapper.UserMapper;
-
-
-
public class Test {
-
-
public static void main(String[] args) {
-
// String resource = "mybatis-config.xml";
-
// try{
-
// //加载Mybatis的配置文件
-
// Reader reader = Resources.getResourceAsReader(resource);
-
// //构建sqlSession的工厂
-
// SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
-
// //创建可以执行sql的SqlSession
-
// SqlSession session = sessionFactory.openSession();
-
// //映射sql的标志字符串
-
// String statement = "com.mybatis.mapper.userMapper.findById";
-
// //执行查询返回id为1的User对象
-
// User user = session.selectOne(statement,1);
-
// System.out.println(user);
-
//
-
// }catch (Exception e) {
-
// e.printStackTrace();
-
// }
-
-
String resource = "mybatis-config.xml";
-
try{
-
//加载Mybatis的配置文件
-
Reader reader = Resources.getResourceAsReader(resource);
-
//构建sqlSession的工厂
-
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
-
//创建可以执行sql的SqlSession
-
SqlSession session = sessionFactory.openSession();
-
UserMapper mapper = session.getMapper(UserMapper.class);
-
User user = mapper.findById(1);
-
System.out.println(user);
-
}catch (Exception e) {
-
e.printStackTrace();
-
}
-
-
-
}
-
}
文章来源: smilenicky.blog.csdn.net,作者:smileNicky,版权归原作者所有,如需转载,请联系作者。
原文链接:smilenicky.blog.csdn.net/article/details/51290605