zoukankan      html  css  js  c++  java
  • small_demo

    数据库设计

    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);
    	}
    
    }
  • 相关阅读:
    VMWare虚拟机非正常关机后无法启动
    curl: (1) Protocol "'https" not supported or disabled in libcurl的解决方法
    spring security入门
    oracle中可以使用drop、delete和truncate三个命令来删除数据库中的表
    com.github.pagehelper:pagehelper:jar:3.4.2-fix.jar
    oracle jdbc驱动 ojdbc14-10.2.0.4.0.jar 网盘下载
    PDF复制SQL语句没有换行符的解决办法
    rpm -qa | grep mysql查询不到MySQL
    新文预览 | IoU-aware Single-stage Object Detector for Accurate Localization
    目标检测 | RetinaNet:Focal Loss for Dense Object Detection
  • 原文地址:https://www.cnblogs.com/sidekick/p/8353701.html
Copyright © 2011-2022 走看看