Mybatis --- XML mapping file

When using the annotation-based Mybatis configuration, please remove the xml mapping configuration (IUserDao.xml).
The following is the xml configuration

Implementation of CRUD operation based on proxy Dao
Requirements:
1. The mapping configuration of the persistence layer interface and the persistence layer interface must be under the same package.
2. The value of the namespace attribute of the mapper tag in the persistence layer mapping configuration must be the fully qualified class of the persistence layer interface. Name
3. The configuration label of the SQL statement, the id attribute of the ,, and must be the same as the method name of the persistence layer interface.
Configured in the user's mapping profile

<insert id="saveUser" parameterType="com.itheima.domain.User">
 insert into user(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address}) 
</insert>

1.
parameterType attribute: represents the type of the parameter, because we want to pass in an object of a class, so the type is written with the full name of the class.
2. The # {} character is used in the SQL statement: It represents a placeholder, which is equivalent to what was learned in the original jdbc part, and is used to replace the actual data when executing the statement.
The specific data is determined by the content in # {}.
3. How to write content in # {}:
Since the parameter of our saving method is a User object, the attribute name in the User object is written here.
It uses ognl expressions.
4.ognl expression: The
syntax format is to use # {object.object} # {user.username} It will first find the user object, then find the username attribute in the user object, and call the getUsername () method to take the value come out. But we specified the entity class name on the parameterType attribute, so we can omit user. And write username directly.
Added return value of user id

//新增用户后,同时还要返回当前新增用户的 id 值,因为 id 是由数据库的自动增长来实现的,所以就相当于我们要在新增后将自动增长 auto_increment 的值返回。
<insert id="saveUser" parameterType="USER">
<!-- 配置保存时获取插入的 id -->
 <selectKey keyColumn="id" keyProperty="id" resultType="int">
select last_insert_id();
</selectKey>
insert into user(username,birthday,sex,address) 
values(#{username},#{birthday},#{sex},#{address})
</insert>

** # {} and $ {} difference **

1. # {} Represents a placeholder symbol.
Through # {}, you can set the preparedStatement to a placeholder, and automatically convert between java type and jdbc type. # {} Can effectively prevent SQL injection. # {} Can receive simple type values ​​or pojo attribute values. If parameterType transmits a single simple type value, # {} can be value or other name in parentheses.
2. $ {} Indicates splicing sql string

Through $ {}, the content passed in parameterType can be spliced ​​in sql without jdbc type conversion

You can also receive simple type values ​​or pojo attribute values. If parameterType transmits a single simple type value, you can only call the name of the value when the parameter is bound in $ {} brackets.

Alias
Basic type and String We can write the type name directly, or use the package name. Class name, for example: java.lang.String. For entity class types, currently only fully qualified class names are used. The reason is that mybaits have registered aliases for commonly used data types during loading, so we can not write the package name when we use it, but our entity class does not Register an alias, so you must write the fully qualified class name. At the same time, when it is the name of the entity class, there is also a requirement that the attribute name in the entity class must be consistent with the column name in the query statement, otherwise encapsulation cannot be achieved.
How to register an alias:

在 SqlMapConfig.xml 中配置:
<typeAliases>
<!-- 单个别名定义 --> <typeAlias alias="user" type="com.itheima.domain.User"/>
<!-- 批量别名定义,扫描整个包下的类,别名为类名(首字母大写或小写都可以) --> <package name="com.itheima.domain"/>
<package name="其它包"/>
</typeAliases>

Passing the pojo packaging object
Passing the query condition through pojo development, the query condition is a comprehensive query condition, including not only the user query condition but also other query conditions (for example, the user purchase product information is also used as the query condition), then you can use the packaging object Pass input parameters.
The Pojo class contains pojo.
Requirement: Query user information based on the user name, and put the query condition in the user attribute of QueryVo.

Define the db.properties file under the classpath

<!-- 配置连接数据库的信息
resource 属性:用于指定 properties 配置文件的位置,要求配置文件必须在类路径下
resource="jdbcConfig.properties"
url 属性:
URL: Uniform Resource Locator 统一资源定位符
http://localhost:8080/mystroe/CategoryServlet URL
协议 主机 端口 URI
URI:Uniform Resource Identifier 统一资源标识符
/mystroe/CategoryServlet
它是可以在 web 应用中唯一定位一个资源的路径-->
 <properties url=file:///D:/IdeaProjects/day02_eesy_01mybatisCRUD/src/main/resources/jdbcConfig.properties">
</properties>
 此时我们的 dataSource 标签就变成了引用上面的配置
<dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>

mappers

1. <mapper resource=" " />使用相对于类路径的资源
如:<mapper resource="com/it/dao/IUserDao.xml" />
注意:此种方法要求 mapper 接口名称和 mapper 映射文件名称相同,且放在同一个目录中。
2. <mapper class=" " />使用 mapper 接口类路径
如:<mapper class="com.it.dao.UserDao"/>
注意:此种方法是注解方式,要求 mapper 接口名称和 mapper 映射文件名称相同 
3. <package name=""/>注册指定包下的所有 mapper 接口
如:<package name="cn.it.mybatis.mapper"/>
Published 43 original articles · Likes2 · Visits 997

Guess you like

Origin blog.csdn.net/study_azhuo/article/details/105435721