今天看啥
    热点:

      天发国际娱乐官网:还有一批电影,趁着《泰囧》的热度,跟风拍出一批公路片,甚至在名字上都要蹭下“囧”字的热度。

      mybatis实战教程一:开发环境搭建,mybatis实战


      步骤一:

      创建一个maven工程,在pom文件中加载依赖

      <dependencies>
          <dependency>
              <groupId>org.mybatis</groupId>
              <artifactId>mybatis</artifactId>
              <version>3.2.1</version>
          </dependency>
          <dependency>
              <groupId>mysql</groupId>
              <artifactId>mysql-connector-java</artifactId>
              <version>5.1.26</version>
          </dependency>
      </dependencies>
      

      步骤二:在MySQL数据库中创建一张表

      CREATE TABLE `user` (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `userName` varchar(50) DEFAULT NULL,
        `userAge` int(11) DEFAULT NULL,
        `userAddress` varchar(200) DEFAULT NULL,
        PRIMARY KEY (`id`)
      ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
      
      Insert INTO `user` VALUES ('1', 'summer', '100', 'shanghai,pudong');
      

      步骤三:创建mybatis的配置文件 configuration.xml

      <?xml version="1.0" encoding="UTF-8" ?>
      <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
              "http://www.1click-soft.com/tfgjylgw/dtd/mybatis-3-config.dtd">
      <configuration>
        <!--定义别名--> <typeAliases> <typeAlias alias="User" type="com.yihaomen.mybatis.model.User"/> </typeAliases> <!--定义数据库信息,默认使用development数据库构建环境--> <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://127.0.0.1:3306/mybatis-learn" /> <property name="username" value="root"/> <property name="password" value="tiger"/> </dataSource> </environment> </environments> <!--定义映射器--> <mappers> <mapper resource="com/yihaomen/mybatis/model/User.xml"/> </mappers> </configuration>

       

      步骤四:新建model

      public class User {
          private int id;
          private String userName;
          private String userAge;
          private String userAddress;
        setters & getters }

       

      user.xml

      <?xml version="1.0" encoding="UTF-8" ?>
      <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
              "http://www.1click-soft.com/tfgjylgw/dtd/mybatis-3-mapper.dtd">
      
      <mapper namespace="com.yihaomen.mybatis.models.UserMapper">
          <select id="selectUserByID" parameterType="int" resultType="User">
              select * from `user` where id = #{id}
          </select>
      </mapper>

       

      下面对这几个配置文件解释下:

      步骤五:测试

      import com.yihaomen.mybatis.model.User;
      import org.apache.ibatis.io.Resources;
      import org.apache.ibatis.session.SqlSession;
      import org.apache.ibatis.session.SqlSessionFactory;
      import org.apache.ibatis.session.SqlSessionFactoryBuilder;
      
      import java.io.IOException;
      import java.io.Reader;
      
      public class Test {
          private static SqlSessionFactory sqlSessionFactory;
          private static Reader reader;
          static {
              try {
                  reader = Resources.getResourceAsReader("configuration.xml");
                  sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      
          public static SqlSessionFactory getSession() {
              return sqlSessionFactory;
          }
      
          public static void main(String[] args) {
              SqlSession session = sqlSessionFactory.openSession();
              try {
                  User user = (User) session.selectOne("com.yihaomen.mybatis.models.UserMapper.selectUserByID", 1);
                  System.out.println(user.getUserAddress());
              } finally {
                  session.close();
              }
          }
      }

       http://www.1click-soft.com/tfgjylgw/huayicompany/springmvc-mybatis

      www.1click-soft.comtruehttp://www.1click-soft.com/Javabc/1229108.htmlTechArticlemybatis实战教程一:开发环境搭建,mybatis实战 步骤一: 创建一个maven工程,在pom文件中加载依赖 dependencies dependency groupIdorg.mybatis/groupId ar...

      相关文章

      相关搜索: mybatis

      帮客评论

      视觉看点
      百度 360 搜狗