• mybatis


    1.导入jar包

    复制代码
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.46</version>
    </dependency>
    
    <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.0</version>
    </dependency>
    
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    </dependency>
    
    <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.12</version>
    </dependency>
    复制代码

    其中log4j需要一个properties配置文件

    # Global logging configuration
    log4j.rootLogger=DEBUG, stdout
    # MyBatis 日志配置
    log4j.logger.mapper.UserMapper=TRACE
    log4j.logger.mapper.OrderMapper=TRACE
    # Console output...
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

    2.编写主配置文件(一般命名SqlMapConfig.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>
    <!-- 和spring整合后 environments配置将废除 -->
    <environments default="development">
    <environment id="development">
    <!-- 使用jdbc事务管理 -->
    <transactionManager type="JDBC" />
    <!-- 数据库连接池 -->
    <dataSource type="POOLED">
    <property name="driver" value="com.mysql.jdbc.Driver" />
    <property name="url"
    value="jdbc:mysql://localhost:3306/名称?characterEncoding=UTF-8" />
    <property name="username" value="数据库名" />
    <property name="password" value="密码" />
    </dataSource>
    </environment>
    </environments>
    <mappers>
    <mapper resource="User.xml"/><!--扫描子mapper文件-->
    <mapper resource="UserMapper.xml"/>
    <mapper class="接口路径<使用注解方式的时候用"/>
    </mappers>
    </configuration>

    建立子mapper.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="接口路径">
    自己改一下下面的 <resultMap id="result_map" type="result"> <id property="studentNo" column="studentNo"/> <id property="subjectNo" column="subjectNo"/> <id property="examDate" column="examDate"/> <result property="studentResult" column="studentResult"/> <result property="stu_Name" column="studentName"/> </resultMap> <select id="queryResultAll" resultMap="result_map"> SELECT r.*,s.`studentName` FROM result r INNER JOIN student AS s ON s.`studentNo`=r.studentNo </select> </mapper>

    或者不需要用子mapper.xml方式而在接口使用注解

    例如:

    @Select("select * from user")
    List<User> select();
    
    @Insert("......")
    int insert(User user);
    
    ......
    View Code

    在测试类中,

    private SqlSessionFactory sqlSessionFactory;
        @Before
        public void init() throws IOException {
            SqlSessionFactoryBuilder sqlSessionFactoryBuilder=
                    new SqlSessionFactoryBuilder();
            InputStream inputStream=
                    Resources.getResourceAsStream("SqlMapConfig.xml");
            this.sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
    
        }
    //通过sqlSessionFactory打开Session,然后调用操作方法
    @Test
    public void selectId(){
    SqlSession sqlSession=sqlSessionFactory.openSession();
    User user=sqlSession.selectOne("selectId",1);
    System.out.println(user.getUsername());
    sqlSession.close();
    }
    @Test
    public void sel(){
    SqlSession sqlSession = sqlSessionFactory.openSession();
    List<User> list=sqlSession.selectList("sel","男");
    for (User user : list) {
    System.out.println(user.getUsername());
    }
    sqlSession.close();
    }
     

    测试-->sqlMapConfig.xml--mapper.xml中的sql语句---mapper---entity

  • 相关阅读:
    浅议tomcat与classloader
    业务系统的JVM启动参数推荐
    Nginx负载趟过的坑
    linux 建立反向shell
    rpm -e 包名 卸载安装的二进制包
    源码安装natcat
    取消密码登录
    ssh-copy-id 安全地复制公钥到远程服务器上
    iptables允许一个ip访问本机的某个端口
    zookeeper 安装 配置集群
  • 原文地址:https://www.cnblogs.com/429lirui/p/13561194.html
Copyright © 2020-2023  润新知