Mybatis on the basis of (a simple function to achieve CURD)

Write mybatis-config.xml file

After creating a project file, first need to use the jar package imports, create a mybatis-config.xml file, source code is as follows:

. 1 < Environments default = "Development">
 2          <Environment ID = "Development">
 . 3              <the transactionManager type = "the JDBC"> </ the transactionManager>
 . 4              <type the dataSource = "the POOLED">
 . 5                  <-! Database driver ->
 6                  <Property name = "Driver" value = "com.mysql.cj.jdbc.Driver" />
 7                  <- - database the uRL of!>
 9                  <Property name = "url" value = "jdbc: MySQL: // ? 127.0.0.1:3306 / ** serverTimezone = UTC "/>
 10                  <-! the following is the database user name and password ->
11                 <property name="username" value="***"/>
12                 <property name="password" value="***"/>
13             </dataSource>
14         </environment>
15     </environments>
16 
17     <mappers>
18         <!--映射文件的地址-->
19         <mapper resource="com/pn/mapper/UserMapper.xml"/>
20     </mappers>

Mapping file UserMapper.xml:

1 <! - corresponding interface ->
 2 <Mapper namespace = "com.pn.mapper.UserMapper">
 . 3      <! - mapping query statement and results objects java ->
 4      <! - for processing field corresponds to the problem query results and java object ->
 5  
6 <-! SQL statement ->
 7      <the SELECT the above mentioned id = "selectUser" resultType = "com.pn.entity.User">
 8          the SELECT * from the User the WHERE userID = # {ID}
 . 9      </ SELECT>
 10  
. 11      <SELECT ID = "a selectAll" the resultType = "com.pn.entity.User">
 12 is          SELECT * from User
 13 is      </ SELECT>
 14  
15      <
insert id="addUser" parameterType="com.pn.entity.User">
16         insert into user(userID,userName,userPassword) values (#{userID},#{userName},#{userPassword})
17     </insert>
18 
19     <update id="updataUser" parameterType="com.pn.entity.User">
20         update user set userName = #{userName},userPassword = #{userPassword} where userID = #{userID}
21     </update>
22 
23     <delete id="deleteUser" >
24         delete from user where userID = #{userID}
25     </delete>
26 </mapper>

To class as SqlSessionFactoryBuilder to parse the configuration file and instantiate sqlSessionFactory, the object is obtained by sqlSession sqlSessionFactory, which is written as a util.

mybatisUtil.java:

. 1  public  class mybatisUtil {
 2      public  static a SqlSessionFactory getSqlSessionFactory () {
 . 3          String Resource = "COM / PN / Resource / MyBatis-the config.xml" ;
 . 4          the InputStream inputStream = null ;
 . 5          the try {
 . 6              // Get stream information profile 
7              = inputStream Resources.getResourceAsStream (Resource);
 . 8          } the catch (IOException E) {
 . 9              e.printStackTrace ();
 10          }
 . 11          //To parse the configuration file by SqlSessionFactoryBuilder class and instantiate SqlSessionFactory 
12 is          a SqlSessionFactory SqlSessionFactory = new new SqlSessionFactoryBuilder () Build (inputStream);.
 13 is          return SqlSessionFactory;
 14      }
 15  
16      public  static the SqlSession the getSession () {
 . 17          a SqlSessionFactory SqlSessionFactory = getSqlSessionFactory ();
 18 is          return sqlSessionFactory.openSession ();
 . 19      }
 20 is }

UserDao.java:

. 1  public  class UserDao {
 2      // get all data 
. 3      public List <the User> the getAll () {
 . 4          the SqlSession the session = mybatisUtil.getSession ();
 . 5          List <the User> userList = session.selectList ( "com.pn.mapper.UserMapper .selectAll " );
 . 6          return userList;
 . 7      }
 . 8      // by querying user ID data 
. 9      public the user getUserByID ( int ID) {
 10          the SqlSession the session = mybatisUtil.getSession ();
 . 11         User = session.selectOne the User ( "com.pn.mapper.UserMapper.selectUser" , ID);
 12 is          Session.close ();
 13 is          return User;
 14      }
 15      // add the user 
16      public  int the addUser (the User User) {
 . 17          = SQLSESSION the SqlSession mybatisUtil.getSession ();
 18 is          int Result = sqlSession.insert ( "com.pn.mapper.UserMapper.addUser", User); // INSERT the return type is an int 
. 19          sqlSession.commit (); / / change the database transaction will be submitted to remember 
20          sqlSession.close ();
 21          return the Result;
22      }
 23      // update user information 
24      public  int updataUser (the User User) {
 25          the SqlSession SQLSESSION = mybatisUtil.getSession ();
 26 is          int Result = sqlSession.update ( "com.pn.mapper.UserMapper.updataUser" , User);
 27          sqlSession.commit ();
 28          sqlSession.close ();
 29          return Result;
 30      }
 31 is      // delete user information corresponding to the ID 
32      public  int deleteUser ( int ID) {
 33 is          the SqlSession SQLSESSION = mybatisUtil.getSession();
34         int result = sqlSession.delete("com.pn.mapper.UserMapper.deleteUser",id);
35         sqlSession.commit();
36         sqlSession.close();
37         return result;
38     }
39 }

Test method test.java:

 1 public static void main(String[] args){
 2         UserDao userDao = new UserDao();
 3         //selectAll
 4         List<User> userList = userDao.getAll();
 5         for(User user:userList){
 6             System.out.println(user.getID()+" "+user.getUsername()+" "+user.getPwd());
 7         }
 8         //通过ID查询用户信息
 9         int id = 1500006;
10         User user  = userDao.getUserByID(id);
11         System.out.println("查询结果:"+user.getID()+" "+user.getUsername()+" "+user.getPwd());
12         //insertUser
13         User user1 =new User();
14         user1.setID(123);
15         user1.setUsername("小明");
16         user1.setPwd("123456");
17         int resultInsert = userDao.addUser(user1);
18         if (resultInsert==1){
19             System.out.println("添加成功。");
20         }else {
21             System.out.println ( "add failed." );
 22 is          }
 23 is          // the updateUser 
24          user1.setPwd ( "111111" );
 25          int resultUpdate = userDao.updataUser (user1);
 26 is          IF (resultUpdate ==. 1 ) {
 27              System.out.println ( "successfully updated." );
 28          } the else {
 29              System.out.println ( "update failed." );
 30          }
 31      }

Attach the project file directory as follows:

 

 There are still some details that need attention during the preparation process.

Guess you like

Origin www.cnblogs.com/BleachCurtain/p/11788088.html