數據庫CRUD操作以及MyBatisd的配置使用
阿新 • • 發佈:2018-09-02
file 操作 name creat 抽象 order by pad wait depend
條件: 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
<?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
• 首頁開發
• 業務字段設計
• 數據庫創建 • CRUD操作 • MyBatis集成 • 註解和XML定義 • ViewObject和DateTool • 首頁開發 • 業務字段設計 實體:• 數據庫創建 GUI版本管理工具創建,然後通過GUI轉SQL; • CRUD操作 insert into table_name (列1, 列2,...) VALUES (值1, 值2,....); select 列名1,列名2 from 表名稱 where 條件;
數據庫CRUD操作以及MyBatisd的配置使用