从persistent.xml获取数据库用户名和密码值

qyswt5oh  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(225)

我正在尝试获取要在应用程序中使用的数据库用户名和密码值。我正在使用entitymanager,但当打印这些值时,我会用星号将它们隐藏起来。如何将这些值隐藏起来而不使用星号?
控制器类

@Controller
public class AttendanceReportController {

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ShiftMasterService shiftMasterService;

    @Autowired
    private EmployeeAttendanceService employeeAttendanceService;

    @PersistenceContext
    EntityManager entityManager;

    @GetMapping("/AttendanceReport")
    public String AttendanceReportPage() {
        return "attendanceReport";
    }

    @ModelAttribute("depList")
    public List<DepartmentMaster> getAllDeps() throws SQLException {

        EntityManagerFactory emf = entityManager.getEntityManagerFactory();
        Map<String, Object> emfProperties = emf.getProperties();

        String url = (String) emfProperties.get("javax.persistence.jdbc.url");
        String user = (String) emfProperties.get("javax.persistence.jdbc.user");
        String password = (String) emfProperties.get("javax.persistence.jdbc.password");
        System.out.println("URL : " + url);
        System.out.println("Username : " + user);
        System.out.println("Password : " + password);

        return departmentService.getAllDep();
    }
}

持久.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="hrm">
<properties>
   <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/hrm"/>
   <property name="javax.persistence.jdbc.user" value="root"/>
   <property name="javax.persistence.jdbc.password" value="1234"/>
   <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>

   <property name="hibernate.show_sql" value="true" ></property>
   <property name="hibernate.format_sql" value="true" />

   <property name="hibernate.jdbc.batch_size" value="30" />
   <property name="hibernate.order_inserts" value="true" />
   <property name="hibernate.generate_statistics" value="false" />

</properties>

</persistence-unit>

</persistence>

输出

URL : jdbc:mysql://localhost:3306/hrm
Username :****
Password :****

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题