Mybatis environment setup

Source: Internet
Author: User

Configuration files mybatis-cofig.xml

<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE configuration PUBLIC "-// mybatis.org//DTD Config 3.0 //" http://mybatis.org/dtd/mybatis-3-config.dtd "> <configuration> <! -- Database Configuration --> <properties resource = "jdbc. properties"/> <typeAliases> <package name = "itat. zttc. shop. model"/> <! -- After a package is set, the following alias does not need to be set individually --> <! -- <TypeAlias type = "itat. zttc. shop. model. User" alias = "User"/> --> <! -- You can use an alias directly in the parameter type of the ing file --> </typeAliases> <environments default = "development"> <environment id = "development"> <transactionManager type = "JDBC"/> <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> <! -- Ing file configuration --> <mappers> <mapper resource = "itat/zttc/shop/model/User. xml"/> </mappers> </configuration>

Ing file User. 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 = "itat. zttc. shop. model. User"> <! -- Namespace is custom --> <insert id = "add" parameterType = "User"> <! -- Itat. zttc. shop. model. user alias --> insert into t_user (username, password, nickname, type) value (# {username}, # {password}, # {nickname}, # {type }) </insert> <update id = "update" parameterType = "User"> update t_user set nickname = # {nickname}, password = # {password }, type = # {type} where id = # {id} </update> <select id = "load" parameterType = "int" resultType = "User"> <! -- ParameterType: parameter type, resultTyp: Return Value Type --> select * from t_user where id =#{ id }; </select> <select id = "list" resultType = "User"> select * from t_user </select> </mapper>

Operation

Public class TestSql {private SqlSession session = null; @ Beforepublic void init () {try {// read the configuration file String resource = "mybatis-config.xml"; InputStream inputStream = Resources. getResourceAsStream (resource); // create sessionSqlSessionFactory factory = new SqlSessionFactoryBuilder (). build (inputStream); session = factory. openSession ();} catch (IOException e) {e. printStackTrace () ;}@ Testpublic void add () {User u = new User (); u. setNickname ("Alumni"); u. setUsername ("ljf"); u. setPassword ("123"); session. insert (User. class. getName () + ". add ", u); session. commit (); session. close () ;}@ Testpublic void load () {User u = session. selectOne (User. class. getName () + ". load ", 3); System. out. println (u. getNickname () ;}@ Testpublic void list () {List <User> list = session. selectList (User. class. getName () + ". list "); System. out. println (list. size () ;}@ Testpublic void test () {System. out. println (User. class. getName ());}}

There is also a common Mapper Operation Method in mybatis.

During data operations, you can use annotations instead of xml to write ing files. Although annotations are commonly used in other frameworks (hibernate and spring), mybatis is more powerful in xml.

Notes for paging:

<! -- Note below: # {param} is used? In the preceding example, replace '', $ {param} complete replacement of the string without ''--> <select id =" find "resultType =" User "parameterType =" map "> select * from t_user where (username like # {name} or nickname like # {name }) order by $ {sort }$ {order} limit # {pageOffset}, # {pageSize} </select>

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.