maven项目使用mybatis+mysql

1.添加依赖,在pom.xml中添加

<!--mybatis核心包-->
<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>3.3.0</version>
</dependency>
<!--MySQL的JDBC驱动包-->
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.29</version>
</dependency>

2. 添加Entity

public class UsersEntity {
  private Integer userid;

  private String username;

  public Integer getUserid() {
    return userid;
  }

  public void setUserid(Integer userid) {
    this.userid = userid;
  }

  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username == null ? null : username.trim();
  }

}

3. 添加dao层的interface UsersEntityMapper.java

public interface UsersEntityMapper {

  UsersEntity getUsersByPhoneNo(String phoneNo);

}

4. 添加mapper的xml UsersEntityMapper.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="com.alphaomega.sunshine.dao.UsersEntityMapper">
  <select id="getUsersByPhoneNo" parameterType="String" resultType="com.alphaomega.sunshine.entity.UsersEntity">
    select * from users where phoneno = #{phoneNo}
  </select>
</mapper>

5. 添加mybatis的配置文件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>
  <environments default="development">
    <environment id="development">
      <transactionManager type="JDBC" />
      <!-- 配置数据库连接信息 -->
      <dataSource type="POOLED">
        <!-- 1.加载数据库驱动 -->
        <property name="driver" value="com.mysql.jdbc.Driver" />
        <!-- 2.数据库连接地址 (&->&amp;)-->
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/schema_name?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=true" />
        <!-- 数据库用户... -->
        <property name="username" value="root" />
        <!-- 数据库密码... -->
        <property name="password" value="xxxxxxx" />
      </dataSource>
    </environment>
  </environments>

  <mappers>
  <!-- 注册文件-->
    <mapper resource="com/alphaomega/sunshine/mappers/UsersEntityMapper.xml"/>
    <mapper resource="com/alphaomega/sunshine/mappers/XxxxxxEntityMapper.xml"/>
  </mappers>

</configuration>

6. mybatis配置文件mybatis_config.xml和UsersEntityMapper.xml的位置

UsersEntityMapper.xml如果放在src/main/java里的话,则需要向pom添加路径,否则package时会报错:找不到xml文件,

原因是maven默认xml文件在src/main/resources里(不过后来发现,即使放在了默认里也不行)。

<build>

  <resources>
    <resource>
      <directory>src/main/java</directory>
      <includes>
        <include>**/*.xml</include>
      </includes>
    </resource>

  </resources>

</build>

向maven项目添加src/main/resources目录,并将mybatis_config.xml放在里面。(虽然可以通过另创建文件夹,然后use as source,但还是按照maven默认比较舒服)

添加的方式可以通过Project-Properties-Java Build Path-Source-Add Folder-选中文件夹后Create new folder...

同样,测试目录src/test/resources也通过这样执行。

问题来了,测试时没有任何问题,但是jar包不能,maven会把test目录的xml文件复制到target/test-class下,但是不会将main下的xml复制到target/class里,

除了那些mapper,因为已经指定了路径。因此还需要在pom的resources标签里指定,为了maven复制过去。

<build>

  <resources>
    <resource>
      <directory>src/main/java</directory>
      <includes>
        <include>**/*.xml</include>
      </includes>
    </resource>

    <resource>
      <directory>src/main/resources</directory>
      <includes>
        <include>**/*.xml</include>
      </includes>
    </resource>

  </resources>

</build>

7. 最后使用

String resouces = "mybatis_config.xml";
InputStream in = Resources.getResourceAsStream(resouces);
//读取配置文件的配置信息,利用SqlSessionFactoryBuilder创建sqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
//利用sqlSessionFactory打开与数据库的会话
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
  UsersEntityMapper ueMapper = sqlSession.getMapper(UsersEntityMapper.class);//通过sqlSession得到mapper
  String no = "13299999999";
  UsersEntity user = ueMapper.getUsersByPhoneNo(no);//调用mapper的方法
  //只读操作不需要sqlSession.commit()
  System.out.println("查询到用户...");
  System.out.println(user.getUsername());
} finally {
  sqlSession.close();
}

posted @ 2019-03-01 11:55  jason47  阅读(5525)  评论(0编辑  收藏  举报