Mybatis simple CRUD Based on XML file configuration, mybatiscrud

Source: Internet
Author: User

Mybatis simple CRUD Based on XML file configuration, mybatiscrud

All the ORM framework learning curves come with a CRUD, which is cool. Now let's get a CRUD. All the configurations are based on the previous one. Let's talk about the code.


<?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.bird.mybatis.bean.userMapper">
<!--CRUD  --><insert id="insertUser" parameterType="com.bird.mybatis.bean.Users">insert into users(name, age) values(#{name}, #{age});</insert>
<delete id="deleteUser" parameterType="int">delete from users where id = #{id};</delete>
<update id="updateUser" parameterType="com.bird.mybatis.bean.Users">update users set name = #{name}, age = #{age} where id = #{id};</update>
<select id="getUser" parameterType="int" resultType="com.bird.mybatis.bean.Users">select * from users where id = #{id};</select>
<select id="getAllUser" resultType="com.bird.mybatis.bean.Users">select * from users;</select></mapper>




Package com. bird. mybatis. bean; 
import java. io. IOException; 
import java. io. reader; 
import java. util. list; 
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 org. junit. before; import org. junit. test;
 public class MyTest {private SqlSessionFactory factory; @ Beforepublic void init () {
String resource = "conf. xml "; Reader reader = null;
 try {reader = Resources. getResourceAsReader (resource);}
 catch (IOException e) {e. printStackTrace ();} 
factory = new SqlSessionFactoryBuilder (). build (reader) ;}
@ Testpublic void testAdd () {
// The default is manual SqlSession session = factory. openSession ();
 String statement = "com. bird. mybatis. bean. userMapper. insertUser "; 
int rows = session. insert (statement, new Users (-1, "Haha", 29);
// submit the session. commit (); 
System. out. println (rows); session. close () ;}
@ Testpublic void testUpdate () {SqlSession session = factory. openSession ();
 String statement = "com. bird. mybatis. bean. userMapper. updateUser "; 
session. update (statement, new Users (4, "Haha", 29); 
session. commit (); session. close () ;}
@ Testpublic void testDelete () {
SqlSession session = factory. openSession ();
 String statement = "com. bird. mybatis. bean. userMapper. deleteUser "; 
session. delete (statement, 1); session. commit (); session. close () ;
}
@ Testpublic void testGetAllUser () {
SqlSession session = factory. openSession ();
 String statement = "com. bird. mybatis. bean. userMapper. getAllUser "; 
List <Users> list = session. selectList (statement); System. out. println (list. size (); 
session. close ();}}

OK, so easy, where is it?



The typeAliases tag in the Configurationxml configuration file of MyBatis can be run even if the program is deleted.

The typeAliases tag is the alias of the object class. After writing, you can write the SQL configuration file, for example, the attribute in the <select> tag, so that you can directly use the alias without writing the specific path of the object, the code can be simplified to show you an example: Write <select resultType = "com. sjh. entity. voteUser "> if you write an alias, you can write the <select resultType =" VoteUsers "> directly write the alias without having to write the object path. VoteUsers can replace" com. sjh. entity. voteUser "is used. If many attributes involve entity classes, it is convenient to directly write aliases. You say that you have deleted the program and run it as usual. When writing the configuration, you must have not applied it to the alias, and all of them have written the actual path. I hope you can understand the purpose of this label through some of my explanations.

Should mybatis write all business logic into the ing xml file?

My personal opinion
The ideal state is to put all the logic in xml, but it depends on the actual situation.
1. If you are creating a product, try to put it in xml as much as possible. However, the logic is complicated and difficult to do.
2. If you want to develop a project quickly, put it in dao.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.