MyBatis study notes a

A Senior Software

1.JDK 8

2.Eclipse March2

3.MySql

4.Navicat

Second, the database SQL command

1. Create a database and specify the encoding

Create database database name default character set utf8

2. Create a table

Create table 表名(

Column Name Type Constraints auto_increment comment 'Remarks'

);

Three, Eclipse use

1. Create a project

1.1 Select the target runtime, or appear new jsp error

1.2 If you forget to choose the right project -> build path-> configure path-> tab library-> fourth add library-> server runtime

2.Eclipse needed to download their own default tomcat simplest structure

Fourth, the naming convention

1. Project Name: Not required, can not afford to Chinese

2. Package: domain name company to write down the edu.swpu

3. Data Access Layer: dao, persist, mapper

4. Entity: entity, model, bean, javabean, pojo

The business logic: service, biz

6. Controller: controller, servlet, action, web

7. Filter: filter

8. Exception: exception

9. Listeners: listener

10. Notes:

Use documentation comments on the 10.1 class and method


/** */

1

In which method 10.2


/ * * / Or //

1

11. categories: large hump

12. The method attributes: a small hump

Five, MVC development model

1.M: Model model, entity classes, business and dao

2.V: view view JSP

3.C: Controller Controller, servlet

3.1 Role: view and logical separation

4.MVC applicable scene: the development of large-scale projects

Example 5. FIG.

5.1 to design database

5.2 the first to write entity class

5.3 Persistence Layer

5.4 business logic

5.5 Controller

5.6 view

Sixth, what framework?

1. Frame: Semi-finished software, developed a set of constraints to solve the problem, be expanded to provide functionality based on

2. The framework code that can not be encapsulated (variable), who need to use a new frame xml file, add a variable in the contents file

2.1 need to establish a specific location and a specific profile name

Xml parsing techniques require the use of 2.2 and reflectometry

3. Common concept

3.1 Library: Class not provide certain logic package

For example: the library is famous aphorism, the introduction of the famous aphorism when writing

3.2 Framework: different from the library, there are some constraints

Example: a fill-frame

Seven, MyBatis Profile

1.Mybatis free open source framework, the original name iBatis, 2010 in google code, moving to github 2013 Nian

2. The effect: the data access layer frame

2.1 of bottom layer is packaged on JDBC

One of the advantages of 3.mybatis:

Need to write the implementation class when 3.1 mybatis, just write sql command to be executed

Eight, environmental structures

1. Import jar

Cglib dependent packages

Dynamic Proxy package

Day Kokorozashitsutsumi

Analytical bytecode package bag is dependent cglib

Day Kokorozashitsutsumi

Day Kokorozashitsutsumi

Day Kokorozashitsutsumi

Mybatis core package

drive

Day Kokorozashitsutsumi

Day Kokorozashitsutsumi

2. Create the global configuration file in the src (write JDBC four variables)

2.1 no name and address requirements

2.2 DTD or schema is introduced in the global configuration file

2.2.1 If there is no prompt to import dtd

Window–>preference–>XML–>XMl catalog–>add按钮

2.3 global configuration file contents


<?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>

<-! Default reference id environment, the currently used environment ->

<environments default="default">

<-! Statement can be used in the environment ->

<environment id="default">

<! - use native JDBC transactions ->

<transactionManager type="JDBC"></transactionManager>

<dataSource type="POOLED">

<property name="driver" value="com.mysql.jdbc.Driver"/>

<property name="url" value="jdbc:mysql://localhost:3306/ssm"/>

<property name="username" value="root"/>

<property name="password" value="password"/>

</dataSource>

</environment>

</environments>

<mappers>

<mapper resource="edu/swpu/mapper/FlowerMapper.xml"/>

</mappers>

</configuration>

Zhengzhou infertility hospital: http: //wapyyk.39.net/zz3/zonghe/1d427.html

3. New to the end of the packet mapper, in the new package: the entity class name + Mapper.xml

3.1 File effect: SQL command to be executed to write

3.2 xml file to understand the implementation class

3.3xml file contents

https://www.xiaohongshu.com/discovery/item/5d7492ce00000000020194c2

<?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">

<- namesapce:! Understood class to implement the full path (package name + class name) ->

<mapper namespace="a.b" >

<- id:! Method name

parameterType: defining parameters Type

resultType: Return Value Type

If the return value is List method, written in the generic List resultType, since the data read mybatis jdbc package, line by line

-->

<select id="selAll" resultType="edu.swpu.pojo.Flower">

select * from flower

</select>

</mapper>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

4. Test results (mybatis used only when used alone, and finally integration ssm when the need to write code below)


InputStream is = Resources.getResourceAsStream("myabtis.xml");

// Use the factory design pattern

SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);

// production SqlSession

SqlSession session = factory.openSession();

List<Flower> list = session.selectList("a.b.selAll");

for(Flower flower : list){

System.out.println(flower.toString());

}

session.close();

1

2

3

4

5

6

7

8

9

10

IX environment to build Detailed

1. The global configuration file contents

1.1 <transactionManager /> attribute values

1.1.1JDBC: transaction management using native JDBC transaction management

1.1.2MANAGED: The Transaction Manager transferred to another container, native JDBC transactions setAutoMapping (false);

1.2 <dataSource /> type attribute

1.2.1POOLED: use the database connection pool

1.2.2UNPOOLED: do not use a database connection pool, and direct use JDBC as

1.2.3JNDI: java naming directory interface technology

Ten, database connection pool

1. an open space in the memory, the database storing a plurality of connection objects

2.JDBC Tomcat Pool, produced directly by a database connection pool tomcat

3. icon

3.1active Status: currently connected is used in an application

3.2Idle idle state: waiting for the application to use

4. Use object database connection pool

4.1 at high frequencies to access the database, using the server database connection pool may reduce the system pressure to improve process efficiency

4.1.1 Small projects NA database connection pool

Step 5. To achieve the JDBC tomcat Pool

5.1 context.xml stored in the META-INF web project, the preparation of the database connection pool related properties in the context.xml


<?xml version="1.0" encoding="UTF-8"?>

<Context>

<Resource

driverClassName="com.mysql.jdbc.Driver"

url="jdbc:mysql://localhost:3306/ssm"

username="root"password="smallming"

maxActive="50"

maxIdle="20"

name="test"

auth="Container"

maxWait="10000"

type="javax.sql.DataSource"

/>

</Context>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

5.2 publish a project to tomcat, the database connection pool created

6 can be obtain using JNDI object database connection pool in java

6.1Context: context interface .context.xml file object type

6.2 Code


Context cxt = new InitialContext();

DataSource ds = (DataSource)

cxt.lookup("java:comp/env/test");

Connection conn = ds.getConnection();

1

2

3

4

6.3 When closing the connection object, the connection object is returned to the database connection pool, the state changes to Idle

XI, three kinds of query

1.selectList () Returns the value of List <resultType attribute control>

1.1 applies to the query results need to traverse demand


List<Flower> list = session.selectList("a.b.selAll");

for (Flower flower : list) {

System.out.println(flower.toString());

}

1

2

3

4

2.selectOne () Returns the value of the Object

2.1 returns results only apply to variable or row of data


int count = session.selectOne("a.b.selById");

System.out.println(count);

1

2

3.selectMap () return value Map

3.1 applies to the need to take in the query results to the needs of the data line by a column value

3.2Map <key, resultType control>


Map<Object, Object> map = session.selectMap("a.b.c", "name");

System.out.println(map);


Guess you like

Origin blog.51cto.com/14510269/2437736