美文网首页
环境搭建

环境搭建

作者: 聪明的小一休 | 来源:发表于2019-08-13 08:32 被阅读0次

    在POM.xml中加入依赖

    <dependencies>
        <dependencies>
            <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.47</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.5.2</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/log4j/log4j -->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.17</version>
            </dependency>
    
            <!-- https://mvnrepository.com/artifact/junit/junit -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
                <scope>test</scope>
            </dependency>
    
      </dependencies>
    

    db.properties加到/TestMybatis/src/main/resources下

    driver=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/javaee?useUnicode=true&characterEncoding=utf8
    username=root
    password=root
    

    创建SqlMapConfig.xml
    说明:
    xml约束文档有两类,dtd xsd

    <?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文件 -->
        <properties resource="db.properties">
    
        </properties>
        <!-- 启动延迟加载 -->
        <settings>
            <setting name="lazyLoadingEnabled" value="true" />
            <setting name="aggressiveLazyLoading" value="false" />
        </settings>
        <!-- 定义别名 -->
        <typeAliases>
            <package name="com.neuedu.model.po" />
        </typeAliases>
    
    
        <!-- 配置数据库连接的信息 -->
        <environments default="test">
            <environment id="test">
                <transactionManager type="JDBC">
    
                </transactionManager>
                <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>
    
        <!-- 注册所有的mapper文件 -->
        <!-- 使用package,要求接口和xml在同包下,且名字相同 -->
        <mappers>
            <!-- <mapper resource="com/neuedu/model/mapper/StudentMapper.xml" /> -->
            <package name="com.neuedu.model.mapper" />
    
        </mappers>
    
    
    </configuration> 
    
    
    
    

    编写接口文件

    package com.neuedu.model.dao;
    import java.util.List;
    import com.neuedu.model.bo.Suser;
    public interface SuserMapper {
        public List<Suser> getAllUsers();
        public void addUser();
        public void deleteUser();
    }
    

    编写Mapper文件

    <?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="com.neuedu.model.dao.SuserMapper" >     
       <select id="getAllUsers" resultType="com.neuedu.model.bo.Suser">
           select * from suser
       </select>
       <insert id="addUser">
            insert into suser(name,age) values ('xx',20)
       </insert>  
       <delete id="deleteUser">
            delete from suser where id = 2
       </delete> 
    </mapper>
    

    构造方法私有

    public class SessionUtils {
        //构造方法私有,外部不能创建对象
        private SessionUtils() {
            
        }
        public static SqlSession getSession() {
            //从ClassPath找SqlMapConfig.xml
            InputStream ins;
            try {
                
                ins = Resources.getResourceAsStream("SqlMapConfig.xml");
                SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(ins);
                //2.通过session工厂获得数据库连接
                SqlSession session=factory.openSession();
                return session;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;        
        }                       
    }
    

    运行测试程序

        public static void main(String[] args) {
            // TODO Auto-generated method stub
            getStudentByPage();
        }
    public  void getStudentByPage() {
            // 获得一个Sqlsession
            SqlSession session = SessionUtils.getSession();
            // 通过session得到一个mapper
            StudentMapper studentMapper = session.getMapper(StudentMapper.class);
            // 调用方法
            List<Student> students = studentMapper.getStudentByPage(0,10);
            for (Student a : students) {
                System.out.println(a.getId() + "\t" + a.getStuname() + "\t" + a.getBirthday());
            }
        }
    

    配置log4j, 将log4.propertis 放在classpath跟路径下

    # Global logging configuration
    log4j.rootLogger=DEBUG,stdout
    # MyBatis logging configuration...
    log4j.logger.org.mybatis.example.BlogMapper=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
    

    结构


    笔记很详细呦

    相关文章

      网友评论

          本文标题:环境搭建

          本文链接:https://www.haomeiwen.com/subject/ulimjctx.html