数据库CRUD操作以及MyBatis的配置使用

news/2024/7/5 5:15:41

• 业务字段设计

• 数据库创建
• CRUD操作
• MyBatis集成
• 注解和XML定义
• ViewObject和DateTool
• 首页开发
 
 
• 业务字段设计
实体:

 

name:logub_ticket
lable:
id  
user_id
ticket
expired
status

 

• 数据库创建
GUI版本管理工具创建,然后通过GUI转SQL;
• CRUD操作
 
insert into table_name (列1, 列2,...) VALUES (值1, 值2,....);
select 列名1,列名2 from 表名称 where 条件;
条件: between 1 and 2 order by 3 desc
UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值
 
• MyBatis集成
1. application.properties增加spring配置数据库链接地址
spring.datasource.url = jdbc:mysql://localhost:3306/wenda?useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.username = root
spring.datasource.password = qwertyuiop
mybatis.config-location = classpath:mybatis-config.xml
同时:在配置的同级别目录下:resources/templates建立粘贴建立官网有:mybatis-config.xml mybatis-config.xml
<? 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 >
 
< settings >
<!-- Globally enables or disables any caches configured in any mapper under this configuration -->
< setting name ="cacheEnabled" value ="true" />
<!-- Sets the number of seconds the driver will wait for a response from the database -->
< setting name ="defaultStatementTimeout" value ="3000" />
<!-- Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn -->
< setting name ="mapUnderscoreToCamelCase" value ="true" />
<!-- Allows JDBC support for generated keys. A compatible driver is required.
This setting forces generated keys to be used if set to true,
as some drivers deny compatibility but still work -->
< setting name ="useGeneratedKeys" value ="true" />
</ settings >
 
<!-- Continue going here -->
 
</ configuration >
2. pom.xml引入mybatis-spring-boot-starter和mysql-connector-java
<dependency>
<groupId> org.mybatis </groupId>
<artifactId> mybatis </artifactId>
<version>1.1.1 </version>
</dependency>
 
< dependency >
< groupId > mysql </ groupId >
< artifactId > mysql-connector-java </ artifactId >
< scope > runtime </ scope >
</ dependency >
 
 
 
3. 官网: http://www.mybatis.org/mybatis3/zh/index.html
DAO层建立modul相关的userDao来通过mybatis访问数据库库:
//注解配置;
@Mapper
public interface UserDAO {
String TABLE_NAME = "user" ;
String INSET_FIELDS = " name, password, salt, head_url " ;
String SELECT_FIELDS = " id, name, password, salt, head_url" ;
//通过抽象,实现crud的复用;
@Insert ( { "insert into " , TABLE_NAME , "(" , INSET_FIELDS ,
") values (#{name},#{password},#{salt},#{headUrl})" } )
int addUser ( User user ) ;
 
@Select ( { "select " , SELECT_FIELDS , " from " , TABLE_NAME , " where id=#{id}" } )
User selectById ( int id ) ;
 
@Select ( { "select " , SELECT_FIELDS , " from " , TABLE_NAME , " where name=#{name}" } )
User selectByName ( String name ) ;
 
@Update ( { "update " , TABLE_NAME , " set password=#{password} where id=#{id}" } )
void updatePassword ( User user ) ;
 
@Delete ( { "delete from " , TABLE_NAME , " where id=#{id}" } )
void deleteById ( int id ) ;
}
//xml配置,在相应的resources下建立DAO相应目录的的xml;
<mapper namespace="com.nowcoder.dao.QuestionDAO"> <sql id="table">question</sql> <sql id="selectFields">id,title, content,comment_count,created_date,user_id </sql> <select id="selectByUserIdAndOffset" resultType="com.nowcoder.model.Question"> SELECT <include refid="selectFields"/> FROM <include refid="table"/>
<if test="userId != 0"> WHERE user_id = #{userId} </if> ORDER BY id DESC LIMIT #{offset},#{limit} </select> </mapper>
 
 
• 注解和XML定义
• ViewObject和DateTool
• 首页开发

 

转载于:https://www.cnblogs.com/liguo-wang/p/9573402.html


http://www.niftyadmin.cn/n/3090019.html

相关文章

java页面乱码_Java Web项目中解决中文乱码方法

Java Web项目中解决中文乱码方法Java具有简单性、面向对象、分布式、健壮性、安全性、平台的独立与可移植性、多线程、动态性等特点。下文是为大家精选的Java Web项目中解决中文乱码方法&#xff0c;欢迎大家阅读参考。第一种情况&#xff1a;调用jsp页面中文显示乱码问题描述&…

手动卸载windows服务

使用windows命令行工具&#xff1a;sc。大名鼎鼎的sc。欲知详细用法&#xff0c;请直接在cmd下键入sc&#xff0c;即显示用法。嘿嘿&#xff0c;很好用的&#xff01;另外&#xff0c;MySql的服务注册其实很简单&#xff0c;即简单地 D:\mysql\bin>mysqld -install Service…

捷账宝常规操作

1.安装流程按“小房子”形状的按钮&#xff0c;进入设备桌面—>打开“拉卡拉应用商店”—>点击左上角搜索按钮&#xff0c;输入“捷账宝”(字不能错&#xff0c;必须一定是这三个字)—>点击右边搜索—>点击“捷账宝”后面的“下载”&#xff0c;下载完成后—>安…

java调用ruby_关于java调用ruby的问题。

java调用ruby脚本&#xff0c;发生错误&#xff1a;LoadError: load error: jopenssl/load -- java.lang.VerifyError: class org.bouncycastle.asn1.ASN1Primitive overrides final method equals.(Ljava/lang/Object;)Zrequire at org/jruby/RubyKernel.java:959require at /…

十五:值类型的装箱和拆箱(一)

简单的说装箱是将值类型转换为引用类型&#xff1b;拆箱是将引用类型转换为值类型&#xff0c;但其内部是怎么实现的&#xff0c;CLR又是如何工作的呢&#xff0c;看下面代码&#xff1a;using System;using System.Collections;struct Point{public Int32 x, y;} public sea…

SDN和互联网厂商与网络厂商之间的爱恨情仇

在上一篇博客中我提到SDN的产生与由来&#xff0c;以及SDN在云网一体化场景中的应用&#xff0c;这期我们来谈谈SDN和互联网厂商与网络厂商之间的情感纠葛。什么是SDN&#xff1f;维基百科中解释说到“是一种抽象控制平面和数据平面的计算机网络方法”&#xff0c;说白了就是可…

asp数据库连接语句

asp数据库连接语句2008-01-03 17:26<一>数据库的连接方法&#xff1a; 1.Access数据库的DSN-less连接方法: set adoconServer.Createobject("adodb.connection") adoconn.Open"Driver{Microsoft Access Driver(*.mdb)};DBQ"& Server.MapPath(&qu…

javascript数组的深度复制解析

2019独角兽企业重金招聘Python工程师标准>>> 对于javascript而言&#xff0c;数组是引用类型&#xff0c;如果要想复制一个数组&#xff0c;包括concat、slice在内的函数&#xff0c;都是浅层复制。也就是说&#xff0c;对于一个二维数组来说&#xff0c;用concat来…