数据库设计
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL COMMENT '用户名',
`password` varchar(32) NOT NULL COMMENT '密码,加密存储',
`phone` varchar(20) DEFAULT NULL COMMENT '注册手机号',
`email` varchar(50) DEFAULT NULL COMMENT '注册邮箱',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COMMENT='用户表';
实体类
public class User implements Serializable {
private Long id;
private String username;
private String password;
private String phone;
private String email;
public User() {
super();
}
public User(Long id, String username, String password, String phone, String email) {
super();
this.id = id;
this.username = username;
this.password = password;
this.phone = phone;
this.email = email;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getusername() {
return username;
}
public void setusername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + ", phone=" + phone + ", email=" + email
+ "]";
}
}
UserMapper接口
public interface UserMapper {
public User selectUserById(Long id);
}
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">
<!-- mybatis全局配置文件configuration: 配置数据源,配置连接池,映射文件 -->
<configuration>
<!-- 引入外部资源文件 -->
<properties resource="db.properties"/>
<!-- <plugins> -->
<!-- <plugin interceptor="com.github.abel533.mapperhelper.MapperInterceptor"> -->
<!-- <property name="IDENTITY" value="MYSQL"/> -->
<!-- <property name="mappers" value="com.github.abel533.mapper.Mapper"/> -->
<!-- </plugin> -->
<!-- </plugins> -->
<!-- environments: 配置多个环境 default:默认使用的环境 -->
<environments default="development">
<!-- environment : 配置单个环境 id : 当前环境的唯一标识-->
<environment id="development">
<!-- transactionManager:事务管理器 mybatis使用原生的jdbc管理事务 -->
<transactionManager type="JDBC"/>
<!-- dataSource:数据源 mybatis有哪些数据源? POOLED JNDI UNPOOLED-->
<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 class package
动态代理Dao开发模式可以使用: class package
-->
<mappers>
<!-- 引入映射资源文件 -->
<mapper resource="UserMapper.xml"/>
<!-- <package name="com.huawei.support"/> -->
</mappers>
</configuration>
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/support-mybatis?useUnicode=true&characterEncoding=utf8&autoReconnect=true
jdbc.username=root
jdbc.password=******
lo4j.properties
log4j.rootLogger=DEBUG,A1
log4j.logger.com.atguigu = DEBUG
log4j.logger.org.mybatis = DEBUG
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c]-[%p] %m%n
UserMapper.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是映射文件配置 :统一定义sql语句标识的. namespace:可以随便定义唯一名称 但是使用mybatis的动态代理dao开发时,有规定特殊的意义(全类路径) -->
<mapper namespace="com.huawei.support.mapper.UserMapper">
<!-- 根据用户id查询用户 -->
<select id="selectUserById" parameterType="Long"
resultType="com.huawei.support.entity.User">
select * from User where id = #{id}
</select>
</mapper>
测试类
public class UserMapperTest {
SqlSessionFactory sqlSessionFactory;
SqlSession sqlSession;
UserMapper userMapper;
@Before
public void setUp() throws Exception {
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
sqlSession = sqlSessionFactory.openSession(true);
userMapper = sqlSession.getMapper(UserMapper.class);
}
@Test
public void test() {
User user = userMapper.selectUserById(2L);
System.out.println(user);
}
}