1)新建maven專案
1.創建一個空工程,new–project–empty project
2.new moudle–maven,配置jdk,選擇模塊quickstart
3.一路next,finish,就會開始自動匯入,這是要耐心等一會
這時如果中途被打斷,重繪maven就可以解決,下圖為重繪maven
點擊compile就可以解決,
4.在main檔案目錄下添加resources檔案并mark as resources file
2)加入maven依賴,修改pom.xml檔案
1.spring依賴
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
2.mybatis依賴
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
3.mysql驅動
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
4.spring事務的依賴
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
5.spring和mybatis的集成依賴
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
6.連接池
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
3)創建物體類
public class Student {
private Integer id;
private String name;
private String email;
private Integer age;
}
4)創建dao介面和mapper檔案
首先開始配置mybatis
dao介面:
import org.omg.PortableInterceptor.INACTIVE;
import zjc.javaweb.entity.Student;
import java.util.List;
public interface StudentDao {
Integer insertStudent(Student stu);
Integer updateStudent(Student stu);
Integer deleteStudentById(Integer id);
List<Student> selectAllStudent();
}
StudentDao.xml(dao介面組態檔)
<?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">
<mapper namespace="zjc.javaweb.dao.StudentDao">
<!--這個位置namespace要改成介面名字相對路徑-->
<insert id="insertStudent">
insert into Student values (#{stuid},#{stuname},#{stuemail},#{stuage})
</insert>
<update id="updateStudent">
update Student set name =#{stuname},
email=#{stuemail},age=#{stuage}
where id=#{id}
</update>
<delete id="deleteStudentById">
delete from Student where id=#{stuid}
</delete>
<select id="selectAllStudent" resultType="zjc.javaweb.entity.Student">
select * from Student;
</select>
</mapper>
mapper檔案
<?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">
<mapper namespace="zjc.javaweb.dao.StudentDao">
<!--這個位置namespace要改成介面名字相對路徑-->
<insert id="insertStudent">
insert into Student values (#{stuid},#{stuname},#{stuemail},#{stuage})
</insert>
<update id="updateStudent">
update Student set name =#{stuname},
email=#{stuemail},age=#{stuage}
where id=#{id}
</update>
<delete id="deleteStudentById">
delete from Student where id=#{stuid}
</delete>
<select id="selectAllStudent" resultType="zjc.javaweb.entity.Student">
select * from Student;
</select>
</mapper>
5)創建mybatis主組態檔
main目錄下創建resources檔案,mark as–resources root
<?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>
<!--這里是設定輸出日志-->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<typeAliases>
<!--物體類的包名-->
<package name="zjc.javaweb.entity.Student"/>
</typeAliases>
<mappers>
<!--將路徑改為寫sql陳述句的xml檔案的路徑,用/,不用.-->
<!--<mapper resource="zjc/javaweb/dao/studentDao.xml"/>-->
<!--是xml檔案的包名-->
<package name="zjc.javaweb.dao"/>
</mappers>
</configuration>
這段代碼就是我們以后可以一直用的mybatis組態檔的模板,學習怎樣使用將一段代碼設定為模板檔案,可以參考我的這篇文章
6)創建service介面和實作類,屬性是dao
這里就開始配置spring了
service介面
package zjc.javaweb.Service;
import zjc.javaweb.entity.Student;
import java.util.List;
public interface StudentService {
Integer addStudent(Student stu);
}
實作類:
package zjc.javaweb.Service;
import zjc.javaweb.dao.StudentDao;
import zjc.javaweb.entity.Student;
import java.util.List;
public class StudentServiceImp implements StudentService{
//為了獲取mybatis的操作
private StudentDao studao;
/* public void setStudao(StudentDao studao) {
this.studao = studao;
}為了set注入賦值,不懂*/
@Override
public Integer addStudent(Student stu) {
int nums=studao.insertStudent(stu);
return null;
}
@Override
public Integer modifyStudent(Student stu) {
return null;
}
@Override
public Integer removeStudent(Integer id) {
return null;
}
@Override
public List<Student> findAllStudent() {
return null;
}
}
7)創建spring的組態檔:宣告mybatis的物件交給spring創建
1.資料源
此處是配置druid資料源
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<!--weile為了便于維護,需要在<property/>的 value 屬性中使用${ },-->
<!--value改成自己資料庫的位置,賬號和密碼-->
<property name="url" value="${jdbc:mysql://localhost:3306/ssm}"/>
<property name="username" value="root"/>
<property name="password" value="333"/>
</bean>
</beans>
2.SqlSessionFactory
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource"/>
<!--value改成classpath:mybatis主組態檔的名字-->
<property name="configLocation" value="classpath:mybatis.xml"/>
</bean>
3.Dao物件
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!--指定dao的路徑-->
<property name="basePackage" value="zjc.javaweb.dao"/>
</bean>
4.宣告自定義的service
<bean id="StudentService" class="zjc.javaweb.Service.StudentServiceImp">
<!--在創建StudentServiceImp實作類時,為了set注入,設定了一個StudentDao的物件StudentDao studao,ref后面就是dao介面的名字-->
<property name="studao" ref="studentDao"/>
</bean>
8)創建測驗類:獲取Service物件,通過Service呼叫dao完成資料庫的訪問,
1.宣告Service,在applicationContext.xml檔案中添加
<bean id="studentService" class="zjc.javaweb.Service.StudentServiceImp">
<property name="studao" ref="studentDao" />
</bean>
@Test
public void test03(){
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
StudentService studentService=(StudentService) ac.getBean("studentService");
Student stu2=new Student();
stu2.setName("zsg");
stu2.setId(3);
stu2.setEmail("521");
stu2.setAge(18);
studentService.addStudent(stu2);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/272211.html
標籤:其他
上一篇:SpringBoot---(4) Spring Boot 集成SSM框架和Dubbo分布式框架
下一篇:Apache 原始碼安裝詳細教程
