ORM簡介
對象關(guān)系映射,是一種程序技術(shù),用于實(shí)現(xiàn)面向?qū)ο缶幊陶Z言里不同類型系統(tǒng)的數(shù)據(jù)之間的轉(zhuǎn)換.從效果上說,它其實(shí)是創(chuàng)建了一個可在編程語言里使用的--"虛擬對象數(shù)據(jù)庫".
ORM的方法論基于三個核心原則:
- 簡單:以最基本的形式建模數(shù)據(jù)
- 傳達(dá)性:數(shù)據(jù)庫結(jié)構(gòu)被任何人都能理解的語言文檔化
- 精確性:基于數(shù)據(jù)模型創(chuàng)建正確標(biāo)準(zhǔn)化的結(jié)構(gòu)
Spring對ORM的框架支持
spring中支持ORM的框架有hibernate、mybatis哄尔、JPA等.但是其中JPA是一種規(guī)范,hibernate和mybatis都遵循JPA規(guī)范,在應(yīng)用中:
建立新的模塊,添加依賴
在資源包中添加連接數(shù)據(jù)庫的文件和配置
jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db_spring?useUnicode=true&useSSL=false&characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
spring_mybatis.xml
<context:property-placeholder location="classpath:jdbc.properties"/>
<context:component-scan base-package="com.spring.orm.service"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName" value="{jdbc.url}"/>
<property name="username" value="{jdbc.password}"/>
<property name="initialSize" value="20"/>
<property name="maxActive" value="20"/>
<property name="minIdle" value="0"/>
<property name="maxWait" value="60000"/>
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<property name="minEvictableIdleTimeMillis" value="300000"/>
<property name="testWhileIdle" value="false"/>
<property name="testOnBorrow" value="false"/>
<property name="testOnReturn" value="false"/>
<property name="poolPreparedStatements" value="true"/>
<property name="maxPoolPreparedStatementPerConnectionSize" value="20"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.spring.orm.entity"/>
</bean>
<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.spring.orm.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="markerInterface" value="com.spring.orm.dao.BaseDAO"/>
<property name="properties">
<value>
mappers = com.spring.orm.dao.BaseDAO
IDENTITY = MYSQL
</value>
</property>
</bean>
<bean id="manager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="manager"/>
完成類中的編寫
User
@Table(name = "t_user")//表單的名稱
@Data
public class User {
//標(biāo)注主鍵和主鍵生成策略
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String account;
private String password;
private Integer credits;
}
BaseDAO
public interface BaseDAO<T> extends Mapper<T>, MySqlMapper<T> {
}
UserDAO
public interface UserDAO extends BaseDAO<User>{
}
UserService
List<User> selectUsers();//查詢所有
int addUsers(User user);//插入
int deleteUser(long id);//刪除
int updateUser(User user);//更新
User selectOneUser(long id);//根據(jù)id查詢
UserServiceImpl
//標(biāo)注本類是一個Service組件
@Service
//在Service層啟動事務(wù)
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
private UserDAO userDAO;
@Override
public List<User> selectUsers() {
return userDAO.selectAll();
}
@Override
public int addUsers(User user) {
return userDAO.insert(user);
}
@Override
public int deleteUser(long id) {
return userDAO.deleteByPrimaryKey(id);
}
@Override
public int updateUser(User user) {
return userDAO.updateByPrimaryKey(user);
}
@Override
public User selectOneUser(long id) {
return userDAO.selectByPrimaryKey(id);
}
}